23

Does Ubuntu support @reboot in crontab?

I find documentation suggesting it is supported, but web chatter asserts it is not.

I cannot make the @reboot section work. The "after midnight" section works fine.

Here is the test example from my /etc/crontab:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
##

# in the beginning - 
@reboot /var/kiosk/btest.sh

# after midnight
30 0     * * *   root    /etc/cron.daily/kiosk/kioskReboot.sh

# end of crontab

shell script btest.sh

#!/bin/bash
date > /var/kiosk/STARTFLAG.txt
echo we booted >> /var/kiosk/STARTFLAG.txt
date
echo we booted

permissions

$ ls -l btest.sh
-rwxrwxrwx 1 root root 147 Aug 21 15:19 btest.sh
$ ls -ld
drwxrwxrwx 14 laptopsanytime root 4096 Aug 21 16:30 .

  • Have you thought about making the script btest.sh, autostart. askubuntu.com/a/9388/2973 – wojox Aug 22 '13 at 1:23
  • There is a great answer on Unix & Linux' [crontab's @reboot only works for root?][1] [1]: unix.stackexchange.com/q/109804/17362 – Édouard Lopez Jul 31 '14 at 13:55
  • I have in my personal crontab @reboot rule that works ok, looks like this: @reboot sh /home/me/run-stuff.sh – taneli Aug 10 '14 at 18:37
  • In case nobody got it: he is mixing up crontabs: /etc/crontab is a user crontab and requires a user. crontab -e is the personal crontab and uses the user that is active at the moment of editing so does not require a username. Both accept @reboot. – Rinzwind Apr 3 at 6:23
21

@reboot is supported in Ubuntu. The reason why your entry

@reboot /var/kiosk/btest.sh

doesn't work in /etc/crontab is because it's missing the user field. The correct syntax would be

@reboot root /var/kiosk/btest.sh
  • 2
    At least in 14.04 this is not the case, and wrong. A "man 5 crontab" does not mention it either. Maybe it depends on the version, but I doubt it. – vorburger Jul 7 '16 at 22:55
  • 1
    @vorburger if you mean @reboot is not supported, then here is a link to wiki that says it does. BTW, on my 14.04, man 5 crontab does tell about @reboot – Fr0zenFyr Jul 15 '16 at 6:29
  • 4
    On my Ubuntu 14.04, to have some user someuser automatically run a command on (re)boot, I had to add to that user's crontab an entry without specifying the username: @reboot /var/kiosk/btest.sh – Abdull Dec 22 '16 at 16:28
  • 1
    Adding to the discussion, I am using ubuntu 16.4.05. When using a users crontab, @reboot command works without specifying the user to run as, and does not when the user is specified. – Keith Reynolds Aug 22 '18 at 2:14
0

It works on 18.04 as job defined in cron.d.
I entered this in the job file:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@reboot root /path/to/script.sh

  • 1
    adding root did not work for me. Did you use crontab -e as root? – MKZ Apr 3 at 6:12
  • I saved it to file start_at_reboot file in the /etc/cron.d directory, – TheR May 28 at 8:07
0

Ubuntu 16.04.5 LTS: As root:

crontab -e

Add the following lines:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@reboot /path_to_script.sh 2>&1 >> /path_to_log.log

2>&1 >> will output stdout to /path_to_log.log so you can troubleshoot this

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.