A pit with the date command and the sudo command under Linux crontab

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.