Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
4  
Crontab seems like a wrong tool for restarting eth0. Why not put that command (without sudo) in /etc/rc.local? – mikewhatever Feb 17 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 W. Feb 17 at 21:17
    
@mikewhatever i can't upvote at the moment but just want to thank you for suggesting that. – Lottie Feb 17 at 21:29
up vote 4 down vote accepted

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).

EDIT: Removed incorrect blurb on absolute paths; kudos @muru

share|improve this answer
    
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 at 21:17
    
cron uses the PATH given in /etc/environment, which does have /sbin. – muru Feb 17 at 21:22
    
I did what @mikewhatever suggested and it worked. thank you – Lottie Feb 17 at 21:29
    
@muru Oh, perhaps I've been laboring on an incorrect assumption; is this answer no longer accurate? – TheSchwa Feb 17 at 21:31
    
@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 at 21:32

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.