0

I'd like to schedule a daily autoshutdown via sudo service autoshutdown start or either abort it via sudo service autoshutdown stop.

/etc/systemd/system/autoshutdown.service

[Unit]
Description=AutoShutdown
[Service]
User = root
Type = oneshot
ExecStart=/etc/systemd/system/autoshutdown.sh
ExecStop=/usr/bin/crontab -r
[Install]
WantedBy=multi-user.target

/etc/systemd/system/autoshutdown.sh

#!/bin/bash
echo "scheduling a shutdown 19:00..."
whoami
crontab -l | cat - /etc/systemd/system/autoshutdown.txt > crontab.txt && crontab crontab.txt
echo "crontab is now"
crontab -l

/etc/systemd/system/autoshutdown.txt

50 18 * * * /sbin/shutdown +10

All amazing. So starting service runs correctly, and when statusing service, I see

[omissis] autoshutdown.sh[11178]: scheduling a shutdown 19:00...

[omissis] autoshutdown.sh[11178]: root

[omissis] autoshutdown.sh[11178]: 50 18 * * * /sbin/shutdown +10

So everything seems ok. However, a latter sudo crontab -l returns an empty table.

"Where" did my crontab go?

Possibly [un]related questions:

  1. Cannot edit crontab as non root user
  2. Cannot edit crontab
  • Does crontab.txt contain what you would expect? – Bodo Apr 30 at 6:38
  • Just refined its absolute path to /etc/systemd/system/crontab.txt and yes, its content is equal to /etc/systemd/system/autoshutdown.txt line – Patrizio Bertoni Apr 30 at 7:03
0

Ok, for the required behaviour, the most fitting option is RemainAfterExit=yes (default: no).

Otherwise, once ExecStart end, ExecStop is immediately executed.

Source: FreeDesktop Systemd Manual

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.