26

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!

PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126
  • 1
    I think this belongs to http://unix.stackexchange.com/ since it's not related to programming. – sknt Apr 06 '17 at 06:26
  • @Skynet oh, i see. so that's the reason for the downvote of other people? :( – PinoyStackOverflower Apr 06 '17 at 10:31
  • I guess. But after a little bit of research i found out that this question is actually a duplicate of this question: http://serverfault.com/questions/817499/when-to-use-sudo-with-crontab (and it incidentally has a downvote too) – sknt Apr 06 '17 at 13:16

2 Answers2

38

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.

Santosh A
  • 5,173
  • 27
  • 37
0

Main part of the problem is to take care of the user with whom you wanna make your things done. Otherwise it will not trigger your cron job. And do make sure that you write output of your command in any file. It will help you to debug the problem which most probably might relate to your relative paths. That's what I faced difficulties in. You can move forward following the below step:

  1. identify your username with which you wanna go. Use whoami command.
  2. Turn to your selected user mode and type crontab -e.
  3. And append line < cron-schedule your-command >> output_filename.cron 2>&1 >
  4. That's it.

Thanks!

Hafiz Hashim
  • 344
  • 3
  • 8