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

  • 7
    Crontab seems like a wrong tool for restarting eth0. Why not put that command (without sudo) in /etc/rc.local? 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

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.

  • 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
  • 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
  • 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. Sep 4 '17 at 2:49

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.

  • 1
    There is no /etc/rc.local file in Ubuntu 20.04... Nov 27 '20 at 4:52
  • @NoopurPhalak create one and make it executable. It will work as usual.
    – muru
    Nov 27 '20 at 4:54

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.