32

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 .

dessert
  • 39,982
user186171
  • 321
  • 1
  • 3
  • 3

3 Answers3

27

@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 07 '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 06: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 02:14
  • Works for me in Ubuntu 18.04.4 - @reboot adb kill-server && sleep 1 && adb devices – PC. Feb 29 '20 at 02:29
  • 1
    @KeithReynolds that is expected and exactly what the documentation says - in a user's crontab cron obviously already knows the user to run as – xeruf Aug 25 '20 at 06:44
1

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

MKZ
  • 131
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

TheR
  • 51
  • 1
  • 4