Reputation: 114035
I am trying to schedule a job on a remote server. I want this job to be scheduled either a minute from the current server time or a minute from the server time of the last scheduled job to run on the server. This way, no two jobs run at the same time (and thus race conditions are avoided).
The job scheduling is done with the at
command on the remote (linux) server. I am forced to use at
because I am running a bunch of complex simulations on several hosts that connect to the same server to request the next simulation (this part has been omitted from my question for brevity).
I am running into problems trying to schedule the job to run at a minute after the last scheduled job (or a minute from now if there are no scheduled jobs). My scheduler script currently looks like this:
minute=`atq | sort -t" " -k1 -nr | head -n1 | cut -d' ' -f4 | cut -d":" -f1,2`
curr=`date | cut -d' ' -f4 | cut -d':' -f1,2`
# the 'python -c" prints the correct scheduling time to stdout
cat <<EOF | at `python -c "import sys; hour,minute=map(int,max(sys.argv[1:]).split(':')); minute += 1; hour, minute = [(hour,minute), ((hour+1)%24,(minute+2)%60)][minute>=60]; print '%d:%02d'%(hour, minute)" "$minute" "$curr"`
python path/to/somescript "$1"
EOF
However, with this script, I get the following error:
somescript: 8: EOF: not found
However, if I were to hardcode the time as follows, that error disappears and the scheduling proceeds as expected:
minute=`atq | sort -t" " -k1 -nr | head -n1 | cut -d' ' -f4 | cut -d":" -f1,2`
curr=`date | cut -d' ' -f4 | cut -d':' -f1,2`
cat <<EOF | at 16:48 # or whatever other time
python path/to/somescript "$1"
EOF
I'd appreciate any help on how to fix this error as my entire setup goes bonkers as a result of this error.
Thank you
Upvotes: 2
Views: 1592
Reputation: 114035
This is what I ended up implementing:
minute=`atq | sort -t" " -k1 -nr | head -n1 | cut -d' ' -f4 | cut -d":" -f1,2`
curr=`date | cut -d' ' -f4 | cut -d':' -f1,2`
# the 'python -c" prints the correct scheduling time to stdout
gotime=`python -c "import sys; hour,minute=map(int,max(sys.argv[1:]).split(':')); minute += 1; hour, minute = [(hour,minute), ((hour+1)%24,(minute+2)%60)][minute>=60]; print '%d:%02d'%(hour, minute)" "$minute" "$curr"`
cat <<EOF | at "$gotime"
python path/to/somescript "$1"
EOF
And it worked like a charm
Upvotes: 0
Reputation: 62499
Rather than cat <<EOF | at ...
, try at <time> <<EOF
. No reason to torture the cat
here.
Upvotes: 1
Reputation: 1722
just a thought. instead of using the here doc why not create a temporary file and get rid of the EOF issue ?
or do this :
echo python path/to/somescript "$1" | at `python -c "import sys; hour,minute=map(int,max(sys.argv[1:]).split(':')); minute += 1; hour, minute = [(hour,minute), ((hour+1)%24,(minute+2)%60)][minute>=60]; print '%d:%02d'%(hour, minute)" "$minute" "$curr"`
would get rid of the EOF, and maybe you could see the error better.
I'm guessing the python code barfs on some weird condition, missing lead zero, am/pm, something that I just don't see at the moment.
Upvotes: 1