0

atUnixコマンドを使用してスケジュールされたタスクを作成しようとしています。私はPythonスクリプトを実行したかったのですがatsh. これを回避するために、コマンドを含むファイルを作成し、代わりにpython mypythonscript.pyそれを渡しましatた。

Python ファイルのアクセス許可を全員が実行できるように設定しました ( chmod a+x) が、atジョブを実行すると、 と通知されpython: can't open file 'mypythonscript.py': [Errno 13] Permission deniedます。

を実行するsource myshwrapperscript.shと、シェル スクリプトが python スクリプトを正常に呼び出します。でパーミッションの問題が発生している明らかな理由はありatますか?

編集: Pythonスクリプトに不満を感じたので、sh実行したいもののスクリプトバージョンを作成しました。私は今、shスクリプトが私に言っていることを発見しましたrm: cannot remove <filename>: Permission denied(これは、中間データを保存するために作成していた一時ファイルでした)。sudo アクセス権がないにもかかわらず、自分の資格情報でこれらの操作を承認できる方法はありますか? これはすべて、私が自分で実行すると完全に機能しますが、実行するとすべてがうまくいかatないようです。

4

4 に答える 4

0

試していただけますか:echo 'python mypythonscript.py' | at ...

于 2012-12-25T20:36:53.977 に答える
0

編集:atコマンドは、シェルコマンドのリストとしてすべてを実行しようとします。したがって、次のようにスクリプトを開始する必要があります。

at now + 1 minute < python mypythonscript.py

この場合、#!スクリプトの先頭の行は必要ありません。

于 2012-12-23T00:58:31.743 に答える
0

最近、サーバーとクライアント間のタスクスケジューリングに取り組んでいます。スケジューリングコードを抽象化してGithubに配置しました。これは、ファイルシステムにすべてのシミュレーションがある複数のマシンにわたって複数のシミュレーションをスケジュールすることを目的としていました。アイデアは、各マシンが異なるプロセッサを持っていたので、各シミュレーションを計算し、結果をサーバーに戻し、サーバーに次のシミュレーションを要求するというものです。サーバーは、次の実行されていないシミュレーションを実行するためにクライアントでタスクをスケジュールすることによって応答します

これがお役に立てば幸いです。

:ファイルを抽象化してアップロードしたのは約5分前なので、抽象化をテストする機会がありませんでした。ただし、バグに遭遇した場合はお知らせください。できるだけ早くデバッグします。

Githubは現在ダウンしているようです。したがって、必要なファイルは次のとおりです。

サーバー上

serverside

#!/bin/bash

projectDir=~/

minute=`atq | sort -t" " -k1 -nr | head -n1 | cut -d' ' -f4 | cut -d":" -f1,2`
curr=`date | cut -d' ' -f4 | cut -d':' -f1,2`

time=`python -c "import sys; hour,minute=map(int,max(sys.argv[1:]).split(':')); minute += 2; hour, minute = [(hour,minute), ((hour+1)%24,minute%60)][minute>=60]; print '%d:%02d'%(hour, minute)" "$minute" "$curr"`

cat <<EOF | at "$time"
python $projectDir/serverside.py $1

EOF

serverside.py

import sys
import time
import smtplib
import subprocess
import os
import itertools

IP = sys.argv[1].strip()
PROJECT_DIR = "" # relative path (relative to the home directory) to the root directory of the project, which contains all subdirs containing simulation files

USERS = { # keys are IPs of the clients, values are user names on those clients
        }

HOMES = { # keys are the IPs of clients, values are the absolute paths to the home directories on these clients for the usernames on these clients  identified in USERS
        }
HOME = None # absolute path to the home directory on the server

SMTP_SERVER = ""
SMTP_PORT = None
FROM_ADDR = None # the email address from which notification emails will be sent
TO_ADDR = None # the email address to which notification emails will be sent

def get_next_simulation():
    """ This function returns a list.
        The list contains N>0 elements.
        Each of the first N-1 elements are names of directories (not paths), which when joined together form a relative path (relative from PROJECT_DIR).
        The Nth element is the name of the file - the simulation to be run.
        Before the end user implements this function, it is assumed that N=3.
        Once this function has been implemented, if N!=3, change the code in the lines annotated with "Change code for N in this line"
             Also look for this annotation in clientside.py and clientsideexec """

    pass

done = False
DIR1, DIR2, FILENAME = get_next_simulation() # Change code for N in this line

while not done:
    try:
        subprocess.check_call("""ssh %(user)s@%(host)s 'sh %(home)s/%(project)/clientside %(dir1)s %(dir2)s %(filename)s %(host)s' """ %{'user':USER, 'host':IP, 'home':HOME[IP], 'project':PRJECT_DIR, 'dir1':DIR1, 'dir2':DIR2, 'filename':FILENAME}, shell=True) # Change code for N in this line
        done = True

        os.remove("%(home)s/%(project)/%(dir1)s/%(dir2)s/%(filename)s" %{'home':HOME, 'project':PROJECT_DIR, 'dir1':DIR1, 'dir2':DIR2, 'filename':FILENAME}) # Change code for N in this line

        sm = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        sm.sendmail(FROM_ADDR, TO_ADDR, "running %(project)s/%(dir1)s/%(dir2)s/%(filename)s on %(host)s" %{'project':PROJECT_DIR, 'dir1':DIR1, 'dir2':DIR2, 'filename':FILENAME, 'host':IP}) # Change code for N in this line
    except:
        pass

クライアントで

clientside

#!/bin/bash

projectpath=~/
python $projectpath/clientside.py "$@"

clientside.py

import subprocess
import sys
import datetime
import os

DIR1, DIR2, FILENAME, IP = sys.argv[1:]
try:
    subprocess.check_call("sh ~/cisdagp/clientsideexec %(dir1)s %(dir2)s %(filename)s %(ip)s" %{'dir1':, 'dir2':, 'filename':, ip':IP}, shell=True, executable='/bin/bash') # Change code for N in this line

except:
    pass

clientsideexec

#!/bin/bash

projectpath=~/
user=''
serverIP=''

SMTP_SERVER=''
SMTP_PORT=''
FROM_ADDR=''
TO_ADDR=''
MESSAGE=''

cat <<EOF | at now + 2 minutes
cd $projectpath/$1/$2  # Change code for N in this line
sh $3

# copy the logfile back to the server
scp logfile$3 $user@$serverIP:$projectpath/$1/$2/
cd $projectpath
python -c "import smtplib; sm = smtplib.SMTP('$SMTP_SERVER', $SMTP_PORT); sm.sendmail('$FROM_ADDR', '$TO_ADDR', '$MESSAGE')"
python clientsiderequest.py
EOF
于 2012-12-23T01:59:59.087 に答える
0

実際のスクリプト名ではなく python を使用してスクリプトを開始します。例: python path/to/script.py.

at は、すべてをshスクリプトとして実行しようとします。

于 2012-12-23T01:09:47.193 に答える