0

I'm trying to create a script that installs sme cron jobs. So far, what I did was to create a template of what I wanted the crontab to look like, and copy it to /var/spool/cron/crontabs/root, overwriting the original file. If you "crontab -e", you can see the cron jobs properly defined, but they are not actually run. Only if you modify something and then exit, forcing crontab to "install" the cron file does it work.

Do I have to additionally, run anything so the new cron file is "parsed" or "installed".

Thanks!

  • Already answered here: stackoverflow.com/questions/878600/… – fox91 Oct 21 '19 at 16:21
  • Longer version of fox91's comment: Avoid editing files in /var/spool unless you're aware of how the existing software is intended to work. There is almost certainly a function of the 'crontab' command or the /etc/cron.d (or similar) directory that will met your need (as described in answers you can find through fox91's link) – Slartibartfast Oct 22 '19 at 3:34
0

Depending on the system you're on, systemd may get your result better than cron because the latter is deprecated.

Write your shell script, save it in /etc as something like shell-script.sh, and make it executable.

Create a .service file in /etc/systemd/system/ called shell-script.service that calls shell-script.sh.

# /etc/systemd/system/shell-script.service

[Unit]
Description=Run shell-script.sh

[Service]
Type=oneshot
ExecStart=-/bin/bash -c /etc/shell-script.sh

Create a .timer file in /etc/systemd/system/ called shell-script.timer:

# /etc/systemd/system/shell-script.timer

[Unit]
Description=Periodically run shell-script.service.

[Timer]
# Run daily at 8:08am and every 4 hours thereafter:
OnCalendar=*-*-* 08/4:08:00
Persistent=true

[Install]
WantedBy=multi-user.target

Finally, start the .timer:

sudo systemctl start shell-script.timer

and enable it so that it's properly compiled to run after reboot:

sudo systemctl enable shell-script.timer
  • How does a non-privileged user use systemd to schedule jobs. For my first try, I ran this systemd-run --on-active=15 /bin/touch ~/blah and got Failed to start transient timer unit: Interactive authentication required. – doneal24 Oct 23 '19 at 0:10

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.