Linux timed shell script execution

Source: Internet
Author: User
Tags echo command

Transferred from: http://blog.csdn.net/jingxiangren/article/details/4745631

In Oracle, the Dbms_job package can be used to perform PL/SQL procedures, which are required at the operating system level, such as backups, to be performed at the OS stage only with crontab.

This article describes the specific usage of crontab to prepare for forgetting.

In Oracle, the Dbms_job package can be used to perform PL/SQL procedures, which are required at the operating system level, such as backups, to be performed at the OS stage only with crontab.

There are roughly three steps to using crontab to perform tasks on a regular basis:

1. Writing shell scripts

2, using Crontab to join the scheduled task queue

3, check the completion of the job

First, how to build shell script

There are many different shells under Linux, but we usually use bash (Bourne again shell) for programming because bash is free and easy to use

The program must start with the following line (must be in the first line of the file):

#! /bin/sh

The symbol #! is used to tell the system that the parameter behind it is the program used to execute the file. In this example we use/BIN/SH to execute the program.

When editing a script, to execute a script, you must make the script executable

The following command allows the script to execute

chmod +x filename

You can then enter the./filename to execute the script.

Note: In shell programming, #符号表示注释, only the end of the line. It is a good idea to use annotations when writing programs.

Variable

All variables in the shell are represented as strings, and the variables are not required to be declared and used directly. Assign values directly to a variable

A= "Hello World"

Remove the variable with the $ sign, such as:

#! /bin/sh

A= "Hello World"

echo "A is:"

Echo $A

The output of the script is executed as follows:

A is:

Hello World

Shell command and Process Control

1) UNIX Commands

You can use any Unix command in the shell, but here are some of the more common UNIX commands

echo "This is a example": Print output to screen

LS: List file lists

CP sourcefile Destinationfile: Copying files

2) Concepts: piping, redirection and Backtick

These are not system commands, but they are often used, very important

Pipeline | The output of one command as input to another command

Grep-qa Compat | More

Redirect: Output command results to a file instead of standard output (screen)

Write files and overwrite old files

The 〉〉 output is appended to the end of the file, preserving the old file.

3) Process Control

If ...; Then

...

else if ...; Then

...

Else

...

Fi

Typically, test commands can be used to test conditions, such as the ability to compare strings, determine whether a file exists and execute permissions, and so on.

Usually "[]" to indicate the condition test, note that the space is important, to ensure that the square brackets space

[-F "somefile"]: Determine if it is a file [-X "/bin/ls"]: Determine if/bin/ls exists and have executable permissions [-n ' $var]: Determine if the $var variable has a value ["$a" = "$b"]: Determine whether a $ A and $b Equal

example general programming steps

Any good script should have help and input parameters, and write a pseudo-script framework.sh, which contains the framework structure that most scripts need.

It's a very good note. At this point, we only need to execute the CP command when we write the script.

CP framework.sh MyScript

And then insert the function you need.

Debugging

The simplest debug command, of course, is to use the echo command. You can use Echo to print any variable value in any place where you suspect it is wrong. That's why most shell programmers spend 80% of their time debugging programs. The advantage of a shell program is that it doesn't need to be recompiled, and it doesn't take much time to insert an echo command.

The shell also has a real debug mode. If there is an error in the script "Strangescript", you can debug it this way:

sh -x strangescript

This executes the script and displays the values of all variables.

The shell also has a pattern that does not need to execute a script just to check the syntax. This can be used:

sh -n your_script

This will return all syntax errors.

Redhat Linux as 3 timed task execution 

Second, cron scheduling process

C R o n is the main scheduling process for the system, which can run a job without human intervention. There is a calledC R o N T a b command allows the user to submit, edit, or delete the corresponding job. Each user can have a C R o n T a b fileTo save the dispatch information. You can use it to run any one S H e l script or a command that runs hourly, or a weekThree times, it all depends on you. Each user can have their own C R o N T a b file, but in a larger system,System administrators generally prohibit these files, and only one such file is retained throughout the system. The system administrator is using theC R o N. d e n y and C R o N. A l l o w these two files to prohibit or allow users to have their own C R o N T a b file.3.1.1 Crontab's DomainTo be able to run a job at a specific time, you need to understand the meaning and format of each field in each entry in the C R o N T a b file.Here are the fields:1th Column min 93 in the 2nd hour (0 means midnight)3rd Liege 14th Column Month 1~1 25th Column Week 0~6 (0 = Sunday)6th List of commands to runSample format for crontab:The following is the format of C R o N T a B:Points < > < > Day < > Month < > Week < > Commands to runwhere < > represents a space.An entry for C R o N T a b file is read from the left, the first column is the minute, the last column is the command to run, it bitAt the back of the week.In these fields, you can use a bar-to represent a time range, for example, if you want to run a work from Monday to Fridayfield, you can use 1-5 in the day of the week to represent it. You can also use the comma "," in these fields, for example, you want Mondayand Thursday to run a job, you only need to use 1, 4来 said. You can use an asterisk * to represent a continuous period of time. If youThere is no special qualification for a field that represents time, and it should be filled in with *. Each entry for this file must contain 5Time domain, and each field is separated by a space. All comment lines in the file are represented by # at the beginning of the row.Crontab Entry ExamplesHere are some examples of C R o N T a b file entries:21* * */apps/bin/cleanup.shThe above example shows that 2 1:3 0 per night run/a p p s/b i n the directory C l e a n u p. S H.4 1,10,22 * */apps/bin/backup.shThe above example shows that the 1, 1 0, 2 2nd of the 4:4 5 run/A p p s/b i n directory b a c K U p. S H.1 * * 6,0/bin/find-name "core"-exec rm {};The above example shows that 1:1 0 per Saturday, Sunday runs an f i n d command.0,30 18-23 * * */apps/bin/dbcheck.shThe above example shows the D b c h e c K in the 1 8:0 0 to 2 3:0 0 run every 3 minutes/a p p s/b i n directory. S H.0 * * 6/apps/bin/qtrend.shThe above example shows the 11:0 0 p m running per Saturday/a p p s/b i n directory of q t r e N d. S H.You may have noticed in the example above that each command gives an absolute path. When using C R o N T a b run s H e l lScript, the user will be given the absolute path of the script, set the appropriate environment variables. Remember, since it is the user to C R o NBy submitting these jobs, you need to provide all the required environment to C R o N. Do not assume that C R o N knows the desired special environment, itactually don't know. So you have to make sure to provide all the necessary path and environment variables in the S H e L script, in addition to some automaticSets the global variable.Command form:The general form of the C R o N T a b command is:Crontab [-u user]-e-l-rwhich-u user name.-e edit C R o N T a b file.-l lists the contents of the C R o N T a b file.-R delete C R o n T a b file.If you log in with your own name, you do not have to use the-u option, because when you execute a c R o N t a b command, the command canKnow the current userBuild a new Crontab fileBefore considering submitting a C R o n T a b file to the C R o N process, the first thing to do is to set the environment variableE D I to R. The C R o N process is based on it to determine which editor to use to edit the C R o N T a b file. 9 9 of U n i x and L I N u x withUsers use V I, and if you do, then you edit the $ H O M e directory. P r o f i l e file in which to add suchLine:Editor=vi; Export EDITORThen save and exit.You may want to create a file called < U S E r > C R o N, where < U S E r > is a user name, for example, J a m e s C R o N. Add the following to the file.#。。。。 Here's the explanation.0,15,30,45 18-06 * * */bin/echo ' time ' >/dev/consoleSave and exit. Make sure that the previous 5 fields are separated by spaces.In the example above, the system will output the current time to the console every 1 to 5 minutes. If the system crashes or hangs,The time shown at the end gives you a glance at what time the system stops working. In some systems, a T-t-y tableThe above example can be modified according to the actual situation.In order to submit the C R o N T a b file you just created, you can take this newly created file as a parameter of the C R o n command:$ crontab JamescronNow that the file has been submitted to the C R o N process, it will run every 1 5 minutes.Also, a copy of the newly created file has been placed in the/V a r/s P o o l/c R o n directory, and the file name is the user name (that is,James).List crontab files:In order to list C R o n T a b files, you can use:You will see something similar to the above. You can use this method to prepare a C R o n T a b file in the H o M e directoryReport$ crontab-l > $HOME/mycronThus, once the C R o N T a b file has been accidentally deleted, it can be recovered quickly using the method described in the previous section.edit crontab File: If you want to add, delete, or edit an entry in a C R o n T a b file, and the E D I to r environment variable is set to V I, then you can use V I to edit the C R o N T a b file, corresponding The command is: $ crontab-e You can modify the C R o N T a b file as you would any other file using V I and exit. If some entries are modified or a new entry is added, C R o N will perform the necessary integrity checks on the file when it is saved. If one of the fields has a value that exceeds the allowable range, it will prompt you. Save and exit. It is best to add a comment above each entry in the C R o N T a b file, so that you can know its function, run time, and more importantly, know which user's job it is. You can use the previous CRONTAB-L command to list all of its information in order to delete C R o n T a b file, you can use: $ crontab-r

Third, you can use the redirect command to output the running result

>,>>

Iv. Specific examples

SH script file: test.sh

#! /bin/sh

Su-oracle << EOF

Sqlplus test/[email protected] @ "Test.sql"

SQL script file: Test.sql

INSERT into TEST_TB values (sysdate);

Crontab file:

0,15,30,45 * * * */apps/bin/test.sh

Linux timed shell script execution

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.