0

I'm just trying to understand the behavior I'm seeing. I tried adding this line to my AWS EC2's (root) crontab:

00 00 * * * shutdown -h now

But this never executed. When I added this to my own ec2-user crontab (including sudo) it did work. I've been able to run different commands from the root crontab, so why can't I run a shutdown command?

4
  • I can agree that you knew when it worked for the user, since a shutdown is rather visible, but how do you know it never executed for root? Maybe it did, but there were some errors? Commented Sep 22 at 13:57
  • When you say that you unsuccessfully tried this in root’s crontab, do you mean root’s personal crontab(crontab -e) or in the system crontab at /etc/crontab? Commented Sep 22 at 16:12
  • 1
    @Kusalananda crontab -e Commented Sep 23 at 14:29
  • 1
    @decision-making-mike You were right, and I chose my words poorly when I said it "never executed". There were errors in /var/log/cron that explained the problem. Commented Sep 23 at 14:38

1 Answer 1

4

I expect this is simply because shutdown isn't in cron's PATH which is just set to /usr/bin:/bin. See Where is cron's PATH set?. So try using the full path to the shutdown executable instead. On my Arch system, it's in /sbin:

$ type shutdown 
shutdown is /sbin/shutdown

So I would use:

00 00 * * * /sbin/shutdown -h now

If that still doesn't work, the way to debug this kind of thing is to redirect the command's output. So try

00 00 * * * shutdown -h now > /root/shutdown.log 2>&1

and check if there is anything in /root/shutdown.log and whether it is created. This, redirecting the command's stdout and stderr, is always the first step when debugging cron stuff.

2
  • Interestingly, on my Arch today, cron's PATH is actually set to /usr/bin:/bin:/usr/sbin:/sbin which wasn't the case back in 2017. Commented Sep 22 at 14:11
  • 2
    That was it! For further supporting evidence, I saw the following in /var/log/cron: CROND[1783]: (root) CMDOUT (/bin/sh: line 1: shutdown: command not found). Commented Sep 23 at 14:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.