Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to run a cron job which needs the sudo command?


 sudo rm somefile
share|improve this question
Welcome Sayem Siam, Have a look at the answers to this question as I think your question has been asked before here on AU – stephenmyall Aug 9 '12 at 17:56
I am trying to execute sudo which needs password but how can i giv password from cron file – sayem siam Aug 9 '12 at 17:59
@sayemsiam you don't need to put sudo, just edit the root crontab. – Braiam Dec 6 '13 at 12:21
up vote 107 down vote accepted

I won't get into how much this is a bad idea; simply put, running sudoin crontab requires your password to be stored somewhere in plaintext.

It's a bad idea.

The following is the preferred method of running administrative tasks through cron. Since you don't really need to write sudo in the crontab, if you are modifying root's crontab.

Use root's crontab

Run the following command:

sudo crontab -e

This opens up root's crontab. sudo is not necessary to run your command in this context, since it'll be invoked as root anyway.

Therefore, you would simply append the following to root's crontab.

@hourly rm somefile

Now, if you absolutely want to be unsafe and take risks with your password, the following will run your command from your own crontab, and enter your password automatically when prompted by sudo.

Again, this is not recommended.

In your own crontab, write your command like so:

@hourly echo "password" | sudo -S rm somefile

The obvious disadvantage here is that, should anyone ever access your crontab, your password will be readable in plaintext.

You shouldn't do this.

share|improve this answer
I got it, thanks a lot – sayem siam Aug 9 '12 at 18:11
Glad it works! Just be wary of any security holes you leave behind.. They might come back later to haunt you. – SirCharlo Aug 9 '12 at 18:15
@SirCharlo Why use root's user crontab instead of the systemwide crontab /etc/crontab? – Eliah Kagan Aug 10 '12 at 3:47
@Elijah why not? – SirCharlo Aug 11 '12 at 4:43

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.

sudo crontab -e
share|improve this answer
I would also place the command in /etc/cron.hourly/something. That's what these directories are for. – John S Gruber Aug 23 '12 at 15:37
No. You could put it in /etc/cron.SOMETHING/SCRIPT, but I wouldn't do both. Both would give roughly the same function, although using crontab you would have a bit more power over how often/when things run. – tgm4883 Aug 23 '12 at 17:40
I should have made clear that I meant that as an alternative. Thanks. – John S Gruber Aug 23 '12 at 17:43

Run following command in terminal

sudo visudo

Added the following line to the end of the file:

vidyadhar  ALL= NOPASSWD: /bin/rm

In the above example vidyadhar is the username and it will not ask for password if you are running rm command through vidyadhar.

share|improve this answer
Hmm.. Then any malicious command, like sudo rm -rf 'slash' (don't run that command), run from that user, would require no password.. I don't know, it feels unsafe, no? – SirCharlo Aug 9 '12 at 18:09
Ya i know it. Your approach is good. But I am using above approach for giving rights to other user to stop/start certain services. – Vidyadhar Aug 9 '12 at 18:13
This is an extremely bad idea. Please don't do this. – bkanuka Jun 29 '14 at 1:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.