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?
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.
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 (
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
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