Operating system: Kylin V4 is based on Ubuntu 15.04

This time, when I configured the crontab on the local operating system Kylin, I found that the configuration was correct, but the crontab did not work.

Configuration method

A file, crontab.conf, is prepared in advance and contains only the following contents, printed every minute

*/1 * * * * echo test

Copy the file to the root crontab configuration

cp crontab.conf  /var/spool/cron/crontabs/root

Run cron-l to find that the configuration is in effect:

root@i-AA788BA8:~# crontab -l
*/1 * * * * echo test

Check the cron log

Tailf /var/log/cron.log # Ubuntu does not enable cron logging by default. You need to manually open /etc/rsyslog.d/50-default.conf to remove cron comments from your tailf. When service rsyslog is restarted, check the log at tailf /var/log/cron.log

At this point, it is found that there is no script running echo test in the log, and the problem cannot be solved by restarting cron

service cron restart

The solution

Try using the original manual configuration scheme and edit it manually with the crontab-e command.

The first time you use the crontab-e system, you will be prompted to select an editor, select any editor

Manually edit any code, such as echo test2, and then save to exit, making sure that the content changes.

When you check the logs, you see that the crontab is running normally.

Mar  2 10:13:01 i-AA788BA8 CRON[29992]: (root) CMD (echo test2 )
Mar  2 10:13:01 i-AA788BA8 CRON[29991]: (CRON) info (No MTA installed, discarding output)
Mar  2 10:14:01 i-AA788BA8 CRON[31354]: (root) CMD (echo test2 )
Mar  2 10:14:01 i-AA788BA8 CRON[31353]: (CRON) info (No MTA installed, discarding output)

This is weird. Do you have to edit it manually to make it work? This is very unfriendly to Linux automation.

At this point, we check the file permissions and see that the manual editing has changed the group of the file. The group has changed from root to crontab.

root@i-AA788BA8:~# ll /var/spool/cron/crontabs/root -rw------- 1 root root 23 Mar 2 10:07 /var/spool/cron/crontabs/root # root @ I - after manual editing AA788BA8: ~ # ll/var/spool/cron/crontabs/root rw -- -- -- -- -- -- -- 1 root crontab 10:07 23 Mar 2 /var/spool/cron/crontabs/root

Let’s test deleting and rebuilding to see if it solves the problem.

rm -rf /var/spool/cron/crontabs/root
cp crontab.conf  /var/spool/cron/crontabs/root
chgrp crontab /var/spool/cron/crontabs/root
root@i-AA788BA8:~# ll /var/spool/cron/crontabs/root
-rw------- 1 root crontab 23 Mar  2 10:07 /var/spool/cron/crontabs/root

At this stage, the problem has been solved theoretically, but it still cannot be run.

Suspicion is the file encoding problem, but through the command also see no difference, try to copy the already normal file as the template file.

cp /var/spool/cron/crontabs/root ~/cron.conf

Then the process of deleting and rebuilding was tested and found to be running normally. It’s possible that the crontab-e command does something about the encoding format of the file.

conclusion

  1. The root configuration file path of the Ubuntu system is at/var/spool/cron/crontabs/root
  2. This file needs the group to be changed to crontab
  3. This file may have a special format, and you need to copy the correct file as a template to automate your work.