4

I'm trying to schedule download at nights. I have created a atd job which starts download script at 2 AM. Now I want to stop the same job at 7 AM if it is still running. What I'm doing now is just shutdown -h 7:00. Is it possible to stop atd job at specified time?

4
  • Stop as in kill them or pause them to be resumed later?
    – slm
    Oct 10, 2013 at 20:03
  • @slm Does it make difference? I thought using "wget -c" will make both options look same. Oct 10, 2013 at 20:06
  • 1
    Why not just have at run killall wget at the specified time?
    – terdon
    Oct 10, 2013 at 20:11
  • Didn't know what exactly you were doing until you mentioned wget, so it's hard for us to say. Perhaps you could expand your Q so we know exactly what you're trying to do. Incidentally it can make a difference b/c we could just have the process go to sleep at 7AM vs. killing it and then resume later on in the day (would be one approach).
    – slm
    Oct 10, 2013 at 20:15

3 Answers 3

2

I don't see a mechanism within at to allow you to specify a stop time. I would just schedule another at job at 7am that would check if any of these at jobs are still running and kill them.

2

At jobs are executed using a shell interpreter (on Linux, it is usually /bin/sh). It means that any command you are going to run as at job can be tracked by a PID number of the shell which is going to run that command. You can simply catch this PID with special shell variable $$. You can save it to a file (single quotes are mandatory to not expand it in your current session shell):

at 'echo $$ > /var/run/my_at_job.pid; your_command; rm /var/run/my_at_jon.pid' | at TIME_SPEC

and later, in another at job, you can kill the job and all its spawned child processes (you_command):

if [ -r /var/run/at_job.pid ]; then
  for PID in $(ps --ppid `cat /var/run/at_job.pid` -o pid | sed -e '1d'); do
    kill $PID
  done
fi
1

You could incorporate the timeout into your job.

timeout "$(($(date +%s -d '7am') - $(date +%s)))" wget -c http://example.com/wibble

Alternatively, have your job write its process ID somewhere so that you can kill it. In the 2am job:

echo $$ >~/.at.download.pid
wget -c http://example.com/wibble
rm ~/.at.download.pid

In the 7am job:

if [ -e ~/.at.download.pid ]; then
  kill $(~/.at.download.pid)
  rm ~/.at.download.pid
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .