12

I have a Raspberry Pi running OSMC (Debian based).

I have set a cron job to start a script, sync.sh, at midnight.

0 0 * * * /usr/local/bin sync.sh

I need to stop the script at 7am. Currently I am using:

0 7 * * * shutdown -r now

Is there a better way? I feel like rebooting is overkill.

Thanks

3 Answers 3

25

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.

5
  • My internet connection is slow hence why it takes so long to sync. Would you recommended any particular signal?
    – Anthony
    Commented Jan 5, 2017 at 16:03
  • @Anthony how do you sync right now?
    – Rabin
    Commented Jan 5, 2017 at 18:49
  • lftp.tech
    – Anthony
    Commented Jan 6, 2017 at 15:56
  • More specifically: gist.github.com/feralhosting/4e7e93a7fecd8f55cb4a
    – Anthony
    Commented Jan 6, 2017 at 16:03
  • @Anthony you can try other application like syncthing (syncthing.net) or BTSync (resilio.com), which work on the block level of the file to sync
    – Rabin
    Commented Jan 7, 2017 at 18:09
9

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

killallpsmisc

2
  • I am using the script found below, with mirror -c it doesn't seem to mind being restarted. Which of the above would you recommend?: gist.github.com/feralhosting/4e7e93a7fecd8f55cb4a
    – Anthony
    Commented Jan 6, 2017 at 16:00
  • Seeing that LFTP script wanting a SIGTERM to clean its lockfile and considering the likelihood of server timeouts on 17 idle hours or even worse inet[64] renumberings, I would go for 0 0 * * * timeout 7h /usr/local/bin/sync.sh or my mostly equivalent killall -TERM variant. Commented Jan 6, 2017 at 16:57
4

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

2
  • Will the Session ID change each time the cron job runs? What about after powering off and on?
    – Anthony
    Commented Jan 5, 2017 at 15:45
  • yes it will change. But if you script it, you can get the PID and place it into a variable. You may be able to just use the name if it shows up. pkill sync.sh
    – ddlingo
    Commented Jan 5, 2017 at 16:03

You must log in to answer this question.

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