Crontab command backup log, crontab command log

Source: Internet
Author: User
Tags rsyslog

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

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.