0

I have a crontab job I am trying to setup: I have this at the moment from reading the documentation and the web.

30 12 * * * reboot 
40 12 * * * python /home/dev/workspace/Testing/__init__.py 
50 12 * * * poweroff

is there someway to randomly change the execution time so make it randomly run between 12:00Pm and 1:00PM?

Also can I tie the poweroff to the python script successfully completing?

Thought I should just clarify I want to leave my computer off and have it automatically start up run the python script then switch itself back off.

Thanks

KillerSnail
  • 3,321
  • 11
  • 46
  • 64

1 Answers1

1

Why are you running this from Crontab anyway? If b should run after a and c after b then just use a; b; c to run them in sequence, no matter how long any one of them takes.

Rebooting out of Cron does not strike me as sane, no matter what problem it is intended to solve, but if you really want to do things that way, you can schedule a reboot and then have an @reboot cron job to pick up when the host comes back up. (You can add a simple check to abort if the host could reboot at other times, too.)

So;

30 12 * * * reboot
@reboot my-reboot-script

where my-reboot-script could be something like

#!/bin/bash

case $(date +%H) in 12) ;; *) exit 0;; esac
sleep $[ $RANDOM % 3600 ]   # bash only
python /home/dev/workspace/Testing/__init__.py
shutdown -P +5

I changed the poweroff command to shutdown -P +5 to give any logged-in users a 5-minute grace period before actually shutting down. I don't know about you, but I would certainly hate to have a computer which can power down without warning while I'm using it.

tripleee
  • 175,061
  • 34
  • 275
  • 318