linux difference between "sudo crontab -e" and just "crontab -e"
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.
Related videos on Youtube
Comments
-
PinoyStackOverflower about 4 years
I noticed that when I typed
sudo crontab -e
I dont see my cron command, but when I do onlycrontab -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 about 7 yearsI think this belongs to unix.stackexchange.com since it's not related to programming.
-
PinoyStackOverflower about 7 years@Skynet oh, i see. so that's the reason for the downvote of other people? :(
-
sknt about 7 yearsI 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-crontab (and it incidentally has a downvote too)
-
-
Santosh A about 7 years@PinoyStackOverflower Welcome. Glad I was able to answer :)
-
Koder101 over 3 years@Santhosh Could you please tell me what's the path of the
sudo crontab -e
andcrontab -e
as I need to add a corn expression programmatically via bash script. -
Santosh A over 3 years@Koder101 /usr/bin/crontab
-
Koder101 over 3 yearsThanks, I figured out the path to be
/var/spool/cron/crontabs/
in Ubuntu forsudo
user -
Usman Liaqat over 2 yearsThat's a good explanation. Thanks @Santosh.