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

PinoyStackOverflower picture PinoyStackOverflower · Apr 5, 2017 · Viewed 18k times · Source

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!

Answer

Santosh A picture Santosh A · Apr 5, 2017

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.