Want to add some task plan in root crontab, want to run command with WWW user, and redirect output to a log file named at that time, roughly as follows
/usr/bin/sudo-u www/usr/local/php/bin/php/www/open/www.php &>/var/log/xxx_cron_log/www_$ (date + "%Y%m%d_%H: %M "). Log
Here are two pits:
1, the customary ' date + '%y%m%d_%h:%m ' and $ (date + "%y%m%d_%h:%m") do not work under crontab, you need to use the following form ' date + ' \%y\%m\%d_\%h:\%m ' and $ (date + "\%y\ %m\%d_\%h:\%m ")
2, directly in the crontab with sudo execution command is invalid, will prompt Sudo:sorry, you must has a TTY to run sudo. Need to modify/etc/sudoers, execute Visudo or vim/etc/sudoers will "D Efaults Requiretty "This line is commented out. Because sudo requires a TTY terminal by default, the commands in Crontab are actually performed in a TTY-free manner. Commenting out "Defaults requiretty" allows Sudo to be executed in a non-terminal manner
However, here are a few things to keep in mind about security :
For this configuration item, the following disable "ssh hostname sudo <cmd>", because it'll show the password in clear. You have the to run "ssh-t hostname sudo <cmd>".
The purpose of this configuration is to disable "ssh hostname sudo <cmd>" because this way the sudo password will be displayed in clear text and you can run "ssh-t hostname sudo <cmd>" instead. On the case that "ssh hostname sudo <cmd>" cannot be executed successfully, after shutting down, there is no check for this layer.
Lenovo: Think of the top command also need to be executed under the terminal, so directly in the shell script called the top command invalid
A pit with the date command and the sudo command under Linux crontab