How to halt a script at a specific time until it is started again by cron?
You can run it with the timeout
command,
timeout - run a command with a time limit
Synopsis
timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]...
timeout [OPTION]
Description
Start COMMAND, and kill it if still running after NUMBER seconds. SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.
PS. If your sync process takes too much time, you might consider a different approach for syncing your data, maybe block replication.
If your sync can easily continue after 17 hours of hibernation, try
0 0 * * * killall -CONT -g sync.sh || /usr/local/bin/sync.sh
7 0 * * * killall -STOP -g sync.sh
If your sync prefers to restart from the beginning, try
0 0 * * * exec /usr/local/bin/sync.sh
7 0 * * * killall -TERM -g sync.sh
If your sync or its subtasks ignore signals but leave no debris, make that
0 0 * * * exec /usr/local/bin/sync.sh
7 0 * * * killall -KILL -g sync.sh
killall
∈ psmisc
I would also use cron to "stop" or "kill" that service or script at a specified time.
First create your cron job to run your job or script.
(you can easily run another cron that will kill a job that has a name
sudo pkill wget
)
Secondly, you want to see running crontab tasks, in a useful and readable format, in the output of:
ps -o pid,sess,cmd afx | egrep "( |/)cron( -f)?$"
They will appear in the first lines, something like this:
1108 1108 cron
4288 1108 \_ CRON
4289 4289 \_ /bin/sh -c /path/to/my/crontab/script1.sh
4290 4289 \_ /bin/bash /path/to/my/crontab/script1.sh
4295 4289 \_ /usr/bin/wget LINK
First column is PID, second is Session ID and third is the command started by cron. You can kill all the processes related to a specific cron task using the Session ID, so in the example above you should kill Session ID 4289:
pkill -s 4289
You need to put the pkill in a script and run this as a cron