Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
350
rated 0 times [  350] [ 0]  / answers: 1 / hits: 381056  / 2 Years ago, tue, may 24, 2022, 2:16:08

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



Like:



 sudo rm somefile

More From » cron

 Answers
7

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.


[#36264] Thursday, May 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
giccolla

Total Points: 161
Total Questions: 124
Total Answers: 117

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
giccolla questions
Sun, Oct 17, 21, 05:39, 3 Years ago
Mon, Aug 1, 22, 02:00, 2 Years ago
Sun, Nov 21, 21, 08:21, 3 Years ago
;