1 background

Today dealt with a small problem with the server of the website that is delegated to Tencent Cloud.

Because I put my resume on GithubPage, but I was not willing to be seen by outsiders, so I configured Nignx to do the reverse proxy to my resume website through Tencent cloud server inside the wall.

However, due to reasons inside the wall, sometimes the ping is not very good, and even DNS can not find the situation. According to o&M specifications, I set the crontab scheduled task of nginx to restart periodically. Sometimes this problem will cause the reverse proxy’s nginx configuration to fail the audit, causing nginx to fail to restart (can only be shut down but not started) and the site to become inaccessible. It’s embarrassing.

In the past, I had to log in to the server and restart nginx manually. Today, I wrote a Shell script to replace the old restart command.

2 Main Steps

  1. Write a script somewhere secure on the server. For example, create a new /etc/myshell folder and write a myshell_1.shshell script under it.

  2. Go to the /etc/myshell folder, run the vim myshell_1.sh command, and write a Shell script as follows:

    #! /bin/bashCopy the code

DATE= DATE “+%Y-%m-%d %H:% m :%S” if ping -c 1 $IP > /dev/null; Then **** # if pings fail else # if pings fail Echo “$DATE $IP ping is failure.” >> /etc/myshell/ping_log.log fi ‘ ‘

Sh 'chmod 755 myshell_1.sh' or 'chmod +x myshell_1.sh' run a test to check whether the Shell script has an error:  ``` ./myshell_1.sh ```Copy the code
  1. Run the crontab -e command to edit the crontab schedule task and add the following command.

    0 3 * * * root /etc/myshell/myshell_1.sh
    Copy the code

    For the crontab command, see the next section “3 Crontab Command Summary”.

  2. complete

3 Summary about the crontab command

With the crontab command, you can execute specified system commands or shell scripts at fixed intervals. The unit of time interval can be any combination of minutes, hours, days, months, weeks or more. This command is suitable for periodical log analysis or data backup. [^ 1]

[^ 1) : Linux Tools Quick tutorial.19. Crontab timed task [EB/OL]. http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html.

3.1. Command format

crontab [-u user] file crontab [-u user] [ -e | -l | -r ]

3.2. Command Parameters

  • -u user: used to set the crontab service of a user.
  • File: File is the name of the command file, indicating that file is used as the crontab task list file and crontab is loaded. If this file is not specified on the command line, the crontab command will accept commands typed on standard input (keyboard) and load them into crontab.
  • -e: edits the crontab file of a user. If no user is specified, the crontab file of the current user is edited.
  • -l: displays the crontab file of a user. If no user is specified, the crontab file of the current user is displayed.
  • -r: deletes the crontab file of a user from the /var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
  • -i: prompts confirmation when deleting the crontab file of the user.

3.3. Crontab file format

Commands to run by hour, day, month, and week

  • Column 1 minutes 0 to 59
  • Column 2 Hours 0 to 23 (0 indicates midnight)
  • Column 3 dates 1 to 31
  • Column 4 from Month 1 to 12
  • Column 5 week 0 to 7 (0 and 7 indicate Sunday)
  • The command to run in column 6

3.4. Common methods

Create a new crontab file

Before submitting a crontab file to the Cron process, you first set the environment variable EDITOR. The Cron process uses this to determine which editor to use to edit the crontab file. 9.9% of UNIX and LINUX users use vi. If that’s you, edit the.profile file in the $HOME directory and add a line like this:

EDITOR=vi; export EDITOR
Copy the code

Then save and exit. Create a file called cron with the user name, for example, Davecron. Add the following to the file.

# (put your own initials here)echo the date to the console every # 15minutes between 6pm and 6am 0,15,30,45 18-06 /bin/echo 'date' > /dev/consoleCopy the code

Save and exit. Note that the first five fields are separated by Spaces.

In the example above, the system prints the current time to the console every 15 minutes. If the system crashes or hangs, you can see at a glance when the system stopped working from the last displayed time. On some systems, the console is referred to as TTy1, and the above example can be modified accordingly. To submit the crontab file you just created, use the newly created file as an argument to the cron command:

$ crontab davecron
Copy the code

Now that the file is committed to the Cron process, it will run every 15 minutes. Meanwhile, a copy of the newly created file has been placed in the /var/spool/cron directory, with the filename being the user name (i.e., Dave).

List the crontab file

List crontab files with the -l argument:

$crontab -l 0,15,30,45 18-06 * * * /bin/echo 'date' > dev/tty1Copy the code

You can use this method to make a backup of the crontab file in the $HOME directory:

$ crontab -l > $HOME/mycron
Copy the code

This way, if you accidentally delete a crontab file, you can quickly recover it using the method described in the next section.

Edit the crontab file

If you want to add, delete, or edit entries in the crontab file and the EDITOR environment variable is set to vi, you can edit the crontab file using vi:

$ crontab -e
Copy the code

You can modify the crontab file as you would any other file with VI and exit. If some entries are modified or new entries are added, cron performs the necessary integrity checks on the file when it is saved. It will alert you if one of the fields has a value that is out of the allowed range. When we edit the crontab file, we might add new entries. For example, add the following:

# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month 30 3,7,14,21,26 * * /bin/ find-name 'core' -exec rm {} \;Copy the code

Save and exit.

It is a good idea to add a comment above each entry in the crontab file so that you know what it does, when it runs, and most importantly, which user is doing the scheduled job.

Delete the crontab file

$crontab -r
Copy the code

3.5. Example

Example 1: Execute myCommand every 1 minute

* * * * * myCommand
Copy the code

Example 2: execution at the 3rd and 15th minutes of every hour

3,15 * * * * myCommand
Copy the code

Example 3: Execute at the 3rd and 15th minutes between 8 a.m. and 11 a.m

3,15 8-11 * * * myCommand
Copy the code

Example 4: The 3rd and 15th minutes are executed every other day from 8 a.m. to 11 a.m

3,15 8-11 */2  *  * myCommand
Copy the code

Example 5: Every Monday from 8 a.m. to 11 a.m. at the 3rd and 15th minutes

3,15 8-11 * * 1 myCommand
Copy the code

Example 6: Restart the SMB at 21:30 every night

30 21 * * * /etc/init.d/smb restart
Copy the code

Example 7: SMB restarts at 4:45 on the first, 10th, and 22nd of each month

45 1,10,22 * * /etc/init.d/ SMB restartCopy the code

Example 8: Restart the SMB at 1:10 every Saturday and Sunday

10 1 * * 6 0 /etc/init.d/ SMB restartCopy the code

Example 9: Restart the SMB every 30 minutes between 18:00-23:00 every day

0,30 18-23 * * * /etc/init.d/ SMB restartCopy the code

Example 10: Restart the SMB every Saturday at 11:00pm

0 23 * * 6 /etc/init.d/smb restart
Copy the code

Example 11: Restart the SMB every hour

* */1 * * * /etc/init.d/smb restart
Copy the code

Example 12: Restart the SMB every hour between 11 p.m. and 7 a.m

0 23-7 * * * /etc/init.d/smb restart
Copy the code

3.6. Precautions for use

Pay attention to environment variables

Sometimes a crontab is created, but the task cannot be executed automatically, and the task can be executed manually with no problem. This is usually caused by the fact that no environment variables are configured in the crontab file.

When defining multiple scheduling tasks in the crontab file, special attention should be paid to the setting of environment variables, because when we manually execute a task, it is carried out in the current shell environment, and the program can certainly find the environment variables, but when the system automatically performs the task scheduling, no environment variables will be loaded. Therefore, You need to specify all the environment variables required to run the task in the crontab file, so that the system can execute the task scheduling without any problems.

Don’t assume that Cron knows about the particular environment required, it doesn’t. So make sure you provide all the necessary paths and environment variables in your shelll script, except for some automatically set global variables. So here are three things to note:

  1. Write global path when file path is involved in script;

  2. When Java or other environment variables are needed for script execution, use the source command to import environment variables, such as:

    cat start_cbp.sh ! /bin/sh source /etc/profile export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf / usr/local/jboss - 4.0.5 / bin/run. The sh - mev & cCopy the code
  3. If the script is executed manually but the crontab is not executed, environment variables are likely to cause the problem. You can directly introduce environment variables to the crontab to solve the problem. Such as:

    0 * * * * . /etc/profile; /bin/sh /var/www/java/audit_no_count/bin/restart_audit.shCopy the code

Clear the mail logs of system users

After each task is scheduled, the system sends the task output information to the current system user in the form of an email. In this case, the accumulated log information is very large and may affect the normal running of the system. Therefore, it is important to redirect each task. For example, you can set the following format in the crontab file to ignore log output:

0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
Copy the code

/dev/null 2>&1 redirects the standard output to /dev/null and then the standard error to the standard output. Since the standard output has been redirected to /dev/null, the standard error will also be redirected to /dev/null, and the log output problem will be solved.

System level task scheduling and user level task scheduling

System-level task scheduling mainly completes system maintenance operations, and user-level task scheduling mainly completes user-defined tasks. You can transfer user-level task scheduling to system-level task scheduling (not recommended), but the reverse is not possible. You can run the crontab -uroot -e command to schedule tasks for the root user, or directly write the task to the /etc/crontab file. Note that if you want to define a task to periodically restart the system, you must put the task in the /etc/crontab file. Even creating a timed restart task as user root is invalid.

Other Matters needing attention

A newly created Cron job will not be executed immediately but will be executed at least two minutes later. If cron is restarted, it is executed immediately.

If crontab fails, run the /etc/init.d/crond restart command to resolve the problem. Tail -f /var/log/cron Check whether a job is executed/an error occurs.

Don’t run crontab -r randomly. It removes the user’s Crontab file from the Crontab directory (/var/spool/cron). All crontabs that deleted the user are gone.

In crontab, % has a special meaning, meaning a line break. If you want to use crontab, you must escape %, for example, date ‘+%Y%m%d’ will not be executed in crontab, you should change it to date ‘+%Y%m%d’.

You need to restart cron after updating the system time zone. In Ubuntu, the service name is cron:

$service cron restart
Copy the code

Start, stop, and restart cron in Ubuntu:

$sudo /etc/init.d/cron start
$sudo /etc/init.d/cron stop
$sudo /etc/init.d/cron restart
Copy the code

4 References

  1. [EB/OL].linuxTools-rst. Readthedocs. IO /zh_CN/lates… .

Nginx blocks malicious IP addresses and periodically canceles two scripts. Tencent Cloud Ubuntu WordPress migration process from Apache to Nginx basic introduction


This article has been published by Tencent Cloud technology community authorized by the author. Please indicate the source of the article when reprinted. For more cloud computing technology dry goods, please go to Tencent cloud technology community