@reboot in crontab

Background: I have an external disk I always mount under /mnt. I use samba and execute the mount -t cifs command using sudo. I would like to have this run at boot time, but the attempts I've made to run it via cron have not been successful. I read that a crontab entry beginning with "@reboot" should work, but it does not. Even under root's crontab.

The command I use that works is

mount -t cifs -o credentials=/etc/samba/creds,uid=1000,gid=1000 //192.168.1.99/HomeStorage /mnt/homestorage

I have tried using the following in root's crontab

@reboot root mount -t cifs -o credentials=/etc/samba/creds,uid=1000,gid=1000 //192.168.1.99/HomeStorage /mnt/homestorage (I added the root user field hoping that might work, but it doesn't with or without it. Any suggestions?

Cron inherits its 'environment' from systemd/init/upstart/runit when it is started, including the PATH variable, but only if it is set by the system (which is usually not the case)

This means that you probably have to give the full path for every command you use like:

/usr/bin/mount -t cifs -o credentials=/etc/samba/creds,uid=1000,gid=1000 //192.168.1.99/HomeStorage /mnt/homestorage

or declare a PATH variable at the beginning of /etc/crontab.

I could be mistaken because the last time i worked extensively with /etc/crontab was before systemd replaced upstart/init. Things might have changed since then.

1 Like

Nice catch! Thank you. I'm guessing I don't have to specify the root user in this case, since this crontrab is root's?

That is correct, /etc/crontab is owned and run by the system c.q. root so you don't have to specify root
:slight_smile:

EDIT: oops...my mistake...you have to specify root (thank you for the correction @ricmarques )

2 Likes

Hi, @OldStrummer (Fred) and @tkn (thom) :slight_smile:

@tkn's suggestion of giving the full path for each command in the crontab was very good! Having said that, let me make a small correction about "/etc/crontab": "/etc/crontab/" is the so-called "system-wide crontab" that requires the username field, unlike the users' crontabs:

$ cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
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 )

If you really want to edit the "root" user crontab instead of the "system-wide crontab", you would run the command sudo crontab -e:

$ sudo crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /usr/bin/vim.gtk3
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [2]: 

If you want to edit your regular user's "crontab" instead, you would do crontab -e (without "sudo"):

$ crontab -e
no crontab for ricmarques - using an empty one
No modification made

I hope this is useful :slight_smile:

2 Likes

Hmmm... What about alternatives, i.e. putting your command and/or script into...

3 Likes