36

I wanted to run a command on our linux after its done rebooting, I saw that it can be done using crontab. I wanted to run this command

sudo ifdown eth0 && sudo ifup -v eth0

on the crontab can i just do:

@reboot sudo ifdown eth0 && sudo ifup -v eth0

or do i need to store that in a script?

thank you

Lottie
  • 393
  • 1
  • 3
  • 8
  • 7
    Crontab seems like a wrong tool for restarting eth0. Why not put that command (without sudo) in /etc/rc.local? – mikewhatever Feb 17 '16 at 21:13
  • What do you need to do this for? These tasks are typically done automatically during the boot process, and almost *NEVER* need to be run separately from the automated boot processes... – Thomas Ward Feb 17 '16 at 21:17
  • 1
    @mikewhatever i can't upvote at the moment but just want to thank you for suggesting that. – Lottie Feb 17 '16 at 21:29

2 Answers2

41

A few notes here before this would work:

  1. Don't use sudo in a cron job. Instead edit root's crontab instead of your own, e.g. sudo crontab -e and then enter commands without sudo.
  2. As @mikewhatever mentioned, this is an odd use for cron, and would likely be better placed in /etc/rc.local before the exit 0 line.
  3. If you tell us exactly what you're looking for, we might be able to direct you to a log or config option (restarting your network services at startup feels a little hackish).
  4. On most systems @reboot should also run after a hard shutdown or crash, but there are different cron implementations so YMMV. I've seen comments in different places asserting both.

EDIT (2016/02/17): Removed incorrect blurb on absolute paths; kudos @muru
EDIT (2016/10/17): Added shutdown note
EDIT (2017/09/11): Revised shutdown note. Not really sure on that one.

TheSchwa
  • 3,820
  • 1
  • 25
  • 38
  • ok just want to clarify, i should just write the full command on `etc/rc.local` without the sudo. so then it will be just ifdown eth0 && sudo ifup -v eth0 exit 0 – Lottie Feb 17 '16 at 21:17
  • `cron` uses the `PATH` given in `/etc/environment`, which does have `/sbin`. – muru Feb 17 '16 at 21:22
  • I did what @mikewhatever suggested and it worked. thank you – Lottie Feb 17 '16 at 21:29
  • @muru Oh, perhaps I've been laboring on an incorrect assumption; is [this answer](http://askubuntu.com/a/110503/205638) no longer accurate? – TheSchwa Feb 17 '16 at 21:31
  • 1
    @TheSchwa Should be. At least since 12.04, `/etc/pam.d/cron` loads `pam_env`, so `/etc/environment` should be read and the default `PATH` set in used. That answer was posted just before 12.04 came out. – muru Feb 17 '16 at 21:32
  • @muru Thanks! Just confirmed that on my own system. I saw that answer on paths a while ago, and just assumed it was correct. Never actually tried making a job without it -.-; – TheSchwa Feb 17 '16 at 21:36
  • 2
    @TheSchwa The statement that cron's reboot doesn't run on a cold boot is wrong. Lines with reboot run when the crond daemon starts, period. That's whether it's a warm boot, cold boot, or whether there was no boot and I just manually shut down crond and started it again. The crond daemon doesn't know or care why it's being restarted, it will simply run reboot lines whenever it does. – Kurt Fitzner Sep 04 '17 at 02:49
  • @KurtFitzner TBF that's not necessarily the case depending on your exact distribution and packages; for example the discuss a file [in this bug report](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=635473) that should theoretically prevent restarting the daemon from running reboot jobs. That being said this answer is from a while ago, and I don't recall what testing I did if any at the time. – TheSchwa Sep 12 '17 at 03:54
  • There is no `/etc/rc.local` file in Ubuntu 20.04... – Noopur Phalak Nov 27 '20 at 04:53
7

You can use crontab for this, but if you use sudo, then you will need a NOPASSWD rule in sudoers to do so. (See How to run a cron job using the sudo command.)

It would be simpler to edit /etc/rc.local and add these commands before the exit 0 line.

muru
  • 193,181
  • 53
  • 473
  • 722