Crontab command backup log, crontab command log
Crontab command to back up logs
Question
James is a server administrator who needs to back up Forum data every day (Here we use logs to replace), back up the logs of the day and delete the previous logs. The file name after the backup is in the format of year-month-day. Alternatives. log is under/var/log.
Target
1. Add scheduled tasks for shiyanlou users
2. Regularly back up alternatives. log to the/home/shiyanlou/tmp/directory at every day.
3. The name format is year-month-day. For example, if today is July 22, the file name is April 1, 2017.
Prompt
Crontab date cp crontab-e sudo crontab-e write a command in crontab without writing a script
Solution
1. First add the tmp directory
mkdir tmp
2. Start rsyslog
sudo service rsyslog start
3. Start crontab
sudo cron -f &
4. Run the following command to add a scheduled task.
crontab -e
5. add the command in the last line of the document to regularly back up alternatives at every day. log to/home/shiyanlou/tmp/directory named in the format of year-month-day
0 3 * * * cp /var/log/alternatives.log /home/shiyanlou/tmp/$(date+ \%Y\%m\%d)
Note:
"%" Is used to end command lines, line breaks, and redirection in the crontab file. The "\" symbol is added before it for conversion. Otherwise, the '%' symbol is used to end the command line or line feed, and the subsequent content is sent to the preceding command as a standard input.
6. After successful addition, we will get a prompt for the last row of installing new crontab.
7. view the tasks we have added.
crontab -l
You can see that the task is correctly saved and successfully added.
8. although we have added a task, if the cron daemon is not started, it will not monitor any task at all, and of course it will not help us to execute it, we can determine whether our cron is successfully started in the background in the following two ways:
ps aux |grep cron
Or
pgrep cron