3

How to execute a script periodically like crontab does but without using crontab?

Please note this that the executed script should not fire with another script and schedule it with 'sleep' command, what I expect to have same functionality as crontab but it should not be present in crontab list and should not be stop when terminal or session ended,

crontab exactly but without croning it.

  • I am afraid if you don' t want to use crontab and don't want to run a script, you are out of options. I guess else crontab would not exist. Possible options might depend on what the script should be triggered by; what defines the period of time. – Jacob Vlijm Nov 2 '16 at 8:01
  • what does the script do exactly? It sounds like you want a kernel module. – j0h Nov 2 '16 at 11:43
5

The modern option is to use a systemd timer unit. This requires creating a systemd unit which defines the job you want to periodically run, and a systemd.timer unit defining the schedule for the job.

Assuming you want to run the job as regular user, put these files in $HOME/.config/systemd/user:

my-job.service

[Unit]
Description=Job that needs periodic execution

[Service]
ExecStart=/path/to/your/script

my-job.timer

[Unit]
Description=Timer that periodically triggers my-job.service

[Timer]
OnCalendar=minutely

Then enable the newly created units, and start the timer:

$ systemctl --user enable my-job.service my-job.timer
$ systemctl --user start my-job.timer

To verify that the timer is set:

$ systemctl --user list-timers
NEXT                         LEFT     LAST                         PASSED UNIT         ACTIVATES
Wed 2016-11-02 14:07:00 EAT  19s left Wed 2016-11-02 14:06:37 EAT  3s ago my-job.timer my-job.service

journalctl -xe should show log entries of the job being run.

Refer to man systemd.timer for the many options for configuring timer behaviour (including randomised starting, waking the computer, persistence across downtime, timer accuracy, etc.), and to man systemd.unit for excellent documentation on systemd and systemd units in general.

2

You could run a script with at and have it reschedule itself as its final instruction. For instance like this:

$ cat > $HOME/my.job <<END
do this
do that
cat "$HOME/my.job" | at now + 5 minutes
END
$ sh ~/my.job

To stop the job, remove it from the at queue:

$ atq
5       Wed Nov  2 11:25:00 2016 a zwets
$ atrm 5

Or simply remove the file my.job so the next execution of cat "$HOME/my.job" will error so at will not be invoked and the rescheduling doesn't happen.

  • ...Which is actually equivalent to sleep 5 mins. – Jacob Vlijm Nov 2 '16 at 8:30
  • @JacobVlijm how is this equivalent? – zwets Nov 2 '16 at 8:33
  • We should wait for OP' s opinion, but I assume he does not literally mean "sleep", but refers to a time interval, set from a script. You might be right however. – Jacob Vlijm Nov 2 '16 at 8:38
  • 1
    @JacobVlijm at does not block and enter sleep, instead it schedules the job for a future time. It is just like cron, but then 'single-shot'. The job isn't attached to a terminal (the way a sleeping bash script would be, unless you disown or nohup it), so survives logout and, more importantly, survives reboot. – zwets Nov 2 '16 at 9:02
  • 1
    could also use at -f "$HOME/my.job" now + 5 minutes – glenn jackman Nov 2 '16 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.