linux difference between "sudo crontab -e" and just "crontab -e"

26,621

Is there a difference between the 2?

Yes, indeed they are different. The difference is that with sudo crontab -e the commands are schedule with root user's credentials. So that the commands in the sudo's cron table are executed as root user.

But with crontab -e, the commands are scheduled with the regular user who is logged in.

Where should I put my cron command, should it be in sudo or without the sudo?

Well, the answer to this depends on the type of command you want to run.
If the command required sudo access then sudo crontab -e should be used.
Else if the cron command doesn't require any special permission then use crontab -e.

Example:
If the ethernet network interface eth0 should be disabled or enabled at specific time then you would use the command
ifconfig eth0 up or ifconfig eth0 down
As the above commands require special permission (sudo), these commands are supposed to added to sudo's cron tab

Any other command which require minimal permission or no permission like removing a file from tmp directory like $ rm /tmp/somefile use the regular user's crontab.

Share:
26,621

Related videos on Youtube

PinoyStackOverflower
Author by

PinoyStackOverflower

Keep on asking to gain more points! :P

Updated on April 14, 2020

Comments

  • PinoyStackOverflower
    PinoyStackOverflower about 4 years

    I noticed that when I typed sudo crontab -e I dont see my cron command, but when I do only crontab -e there is my command.

    Is there a difference between the 2? If there is, where should I put my cron command, should it be in sudo or without the sudo?

    Thanks!

    • sknt
      sknt about 7 years
      I think this belongs to unix.stackexchange.com since it's not related to programming.
    • PinoyStackOverflower
      PinoyStackOverflower about 7 years
      @Skynet oh, i see. so that's the reason for the downvote of other people? :(
    • sknt
      sknt about 7 years
      I guess. But after a little bit of research i found out that this question is actually a duplicate of this question: serverfault.com/questions/817499/when-to-use-sudo-with-cront‌​ab (and it incidentally has a downvote too)
  • Santosh A
    Santosh A about 7 years
    @PinoyStackOverflower Welcome. Glad I was able to answer :)
  • Koder101
    Koder101 over 3 years
    @Santhosh Could you please tell me what's the path of the sudo crontab -e and crontab -e as I need to add a corn expression programmatically via bash script.
  • Santosh A
    Santosh A over 3 years
    @Koder101 /usr/bin/crontab
  • Koder101
    Koder101 over 3 years
    Thanks, I figured out the path to be /var/spool/cron/crontabs/ in Ubuntu for sudo user
  • Usman Liaqat
    Usman Liaqat over 2 years
    That's a good explanation. Thanks @Santosh.