Score:1

Sudo crontab -e vs crontab -e running command as sudo

cn flag

Say I need to run a php script with root permissions every night, is it better practice to run it from the root crontab, or from user with sudo permissions crontab, 'sudoing' in the command. i.e.

In sudo crontab -e

0 0 * * * /bin/sh -c "/[my_path_to]/php /[my_path_to]/[my_script].php >> /[my_path_to]/cron_log.txt 2>&1"

OR

In crontab -e

 0 0 * * * sudo su -s /bin/sh -c "/[my_path_to]/php /[my_path_to]/[my_script].php >> /[my_path_to]/cron_log.txt 2>&1"

Or is there effectively no difference?

user535733 avatar
cn flag
"Better" is the way that you understand and can maintain.
sudodus avatar
jp flag
`sudo crontab -e` is the way things are set up to be used (in order to run scheduled tasks with elevated permissions. But if you make it work the other way, it is also OK. But don't ask me if you have problems to get the password into sudo without compromising the security.
muru avatar
us flag
You don't really need that `sh -c` invocation in root's crontab
Score:1
cn flag

Of course, it is better to run a command that needs to be run as root from the crontab of the root user.

Running a command with sudo in the user's crontab can technically work, but will require you implement a way to supply the sudo password to that command: you will create issues with security.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.