Shell Programming Learning

Source: Internet
Author: User

1. Crontrab for Linux in the project

Scheduled execution under Linux is mostly done using a custom plan in the crontab file, but it's not very complex, and it's basically remembered once and for all, and the key is to remember the/var/spool/cron directory. Here's a look at the specific usage:

First Look at the/etc/crontab file: $ cat/etc/crontab Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly
The first four rows are the environment variables that set the cron task to run. The value of the shell variable specifies the shell environment used by the system (the example is Bash shell), which defines the path to execute the command. The output of Cron is sent as an e-mail message to theuser name defined by the M ailto variable. If the MAILTO variable is defined as an empty string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the base directory when executing a command or script. Note:"run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.
Each line of the task in the file/etc/crontab is described in the following format:
minute hour day month DayOfWeek command   minute -integers from 0 to 59
hour -integers from 0 to 23
Day-an integer from 1 to 31 (must be a valid date for the specified month)
month -an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation)
DayOfWeek -integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand)
Command-commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts)
Root indicates to run as root user
Run-parts means a folder followed by all the scripts under that folder   For each of these statements, the asterisk (*) represents all available values. For example, when referring to month, the command is executed monthly (subject to other restrictions ).
The hyphen (-) between integers denotes an integer sequence , for example 1-4 means integers 1,2,3,4
The specified value is separated by commas. Such as: 3,4,6,8 represents these four specified integers.
the symbol "/" specifies the stepping setting. "/" indicates a stepping value. such as the 0-59/2 definition is executed every two minutes. The step value can also be represented by an asterisk. such as */3 is used to run a specified task every three months running.  A comment line that begins with "#" is not executed. Add a weekly alternate day backup method: 0,1,3,5 command #周一三五七  If a cron task needs to be performed on a regular basis instead of by the hour, day, week, and month, you need to add the/ETC/CRON.D directory. All files and files in this directory are/etc/crontab in the same syntax, see the sample:  # Record the memory usage of the system every Monday
# at 3:30AM in the File/tmp/meminfo
3 * Mon cat/proc/meminfo >>/tmp/meminfo
# Run Custom Scrīpt the first day of every month at 4:10AM
4 1 * */root/scrīpts/backup.sh The Cron service checks for changes in/etc/crontab,/etc/cron.d/,/var/spool/cron files every minute. If a change is found, it is downloaded to the memory. Therefore, even if the crontab file changes, the program does not need to be restarted. The recommended custom task is added using the CRONTAB-E command, which restarts the crond process with the/etc/init.d/crond Restart command, and the official file says that it does not restart the process, but I am not able to run the task without restarting. Start do not know/etc/crontab file run-parts what meaning, direct command in accordance with the/etc/crontab format plus always can not run, later only know run-parts refers to the folder followed by. Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways: /sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload configuration You can also start the service automatically when the system starts, adding:/sbin/service crond start at the end of the/etc/rc.d/rc.local script Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use: 1. Edit directly with crontab command The cron Service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:crontab-u//Set a user's Cron service, which is usually required by the root user when executing this commandcrontab-l//list details of a user cron servicecrontab-r//delete a cron service with no userscrontab-e//Edit a user's cron serviceFor example, root to view your cron settings: crontab-u root-lagain, for example, Root wants to delete Fred's cron settings: Crontab-u fred-rwhen editing the cron service, the edited content has some formatting and conventions, input: Crontab-u root-eEnter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt note >> represents append at the end of the file, and > represents the overlay;
See if the service is already running with Ps-ax | grep cron View scheduling TasksCrontab-l//List all current Scheduled Tasks Crontab-l-u JP//List all scheduled tasks for user JP Delete Task Dispatch task Crontab-r//Delete all Tasks scheduling task scheduling execution results Steering Example 1: Execute the LS command 5:30 every day and output the results to the/jp/test file 5 * * ls >/jp/test 2>&1 Note: 2>&1 indicates execution result and error false informationWhen a job is run in the foreground, the terminal is occupied by the job, and when the job is run in the background, it does not occupy the terminal. You can use the & command to place jobs in the background.
Such as:
2 * * */data/app/scripts/hotbackup/hot_database_backup.sh & 0 2 * * */u01/test.sh >out.file 2>&1 &
in this example, 2>&1 indicates that all standard output and error outputs will be redirected to a file called Out.file. Here are a few numbers that mean:
0 indicates keyboard input
1 indicates standard output
2 indicates an error output. 0 2 * * */u01/test.sh >/dev/null 2>&1 &
This means that the command is executed in the background and the error output 2 is redirected to standard output 1, and then the standard output 1 is all placed in the/dev/null file, which is empty.
in Crontab, the% has a special meaning, indicating the meaning of the line break. If you want to use the words must be escaped \%, such as the frequently used date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +\%y\%m\%d '.

In Linux, use Echo $$ as the current shell's process number.
You can use PS-A to see your own Shell's PID.

$ #传递到脚本的参数个数
$* displays all parameters passed to the script in a single string, which, unlike positional variables, can have more than 9 parameters
$$ the current process ID number for the script to run
$! Process ID Number of the last process running in the background
[Email protected] and $ #相同, but use quotation marks and return each parameter in quotation marks

$-shows the current options used by the shell, same as the SET command function
$? Displays the exit status of the last command. 0 means no error, and any other value indicates an error

$ A is the name of the script itself. $ is the first parameter passed to the shell script, and the second argument that is passed to the shell script, such as the script content, is as follows: #!/bin/shecho "number:$#" echo "scname:$0"

From: Http://man.linuxde.net/read

From: Http://man.linuxde.net/read
Standard input (stdin): code 0, using < or <<
Standard output (STDOUT): Code 1, using > or >>
Standard error Output (STDERR): Code 2, using 2> or 2>>
For example: echo "Hello" >/dev/null.

File expression
-e filename true if filename exists
-d filename If filename is a directory, true
-F filename True if filename is a regular file
-L filename True if filename is a symbolic link
-r filename If filename is readable, true
-W filename if filename is writable, true
-x filename If filename is executable, true
-S filename true if the length of the file is not 0
-H filename True if the file is a soft link
Filename1-nt filename2 If filename1 is newer than filename2, it is true.
Filename1-ot filename2 If filename1 is older than filename2, it is true.

An integer variable expression-eq equals-ne not equal to-gt greater than-ge greater than or equal to-lt less than-le less than equals
string-Variable expression
if [$a = $b] true if string1 equals string2
                                string allows the use of an assignment number Do equals if  [$string 1! =   $string 2]   true       if  [-N if string1 is not equal to string2 $string  ]             If string is non-null (not 0), returns 0 (true)  if  [-Z $string  ]   &NB Sp         If string is empty, true if  [$sting]                 &NB SP; returns 0 (and-N) if string is not NULL   logical non!                  The opposite of the conditional expression
if [!] Expression]if [!-D $num] if the directory does not exist $num
The juxtaposition of logical and –a conditional expressions
If [expression 1–a-expression 2]
Logical OR-o conditional expression or
If [expression 1–o-expression 2]

Shell Programming Learning

Related Article

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.