Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 539
PinoyStackOverflower Avatar asked Apr 05 '17 17:04

PinoyStackOverflower


People also ask

Do you need to use sudo in crontab?

If you are putting the script from one of the cron directories ( /etc/cron. * ) then you don't need to use sudo as that is running as root. If you are using crontab, then you will want to use root's crontab. This will run it as root, and also not need sudo.

What is crontab E?

The crontab command submits, edits, lists, or removes cron jobs. A cron job is a command run by the cron daemon at regularly scheduled intervals. To submit a cron job, specify the crontab command with the -e flag. The crontab command invokes an editing session that allows you to create a crontab file.

Are cron and crontab the same?

cron is the name of the tool, crontab is generally the file that lists the jobs that cron will be executing, and those jobs are, surprise surprise, cronjob s.

Does crontab run as root?

Like any other user, root has a user crontab. Essentially the same as any other user crontab, you are editing the root crontab when you run sudo crontab -e . Jobs scheduled in the root user crontab will be executed as root with all of its privileges.


1 Answers

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.

like image 167
Santosh A Avatar answered Sep 28 '22 10:09

Santosh A