Linux Shell Programming Real-combat skills

Source: Internet
Author: User

Avoid common problems with timed task scripts

Many scripts are actually used in the form of timed tasks rather than hand-run. However, scripts that implement the same functionality may encounter different problems in both modes of operation.

A script that runs as a timed task often encounters several problems.

Path problem: The current directory is often not the directory where the script file resides. As a result, scripts cannot easily use relative paths when referencing external files that they use, such as configuration files and other script files.

The command cannot find a problem: some of the external commands used in the script can be called normally when the script is executed manually. However, running under a scheduled task may present a problem where the script parser cannot find the relevant command.

Script recurring problem: The execution of one script does not end, and the next script is run. Causes multiple processes in the system to run the same script at the same time.

Here's how to share some of these common problems with the development of timed task scripts.

Path issues

The current path under the scheduled task is often not the directory where the script file resides. So we need to refer to it with an absolute path. That is, get the directory where the script resides, and then use an absolute path based on that directory to refer to the external files required by the script. method as shown in the following code.

Listing 1. Get the path where the script file resides

#!/usr/bin/ksh
    
echo "Current path is: ' pwd '"
scriptpath= ' dirname $ ' #获取脚本所在路径
    
echo "The script is located at : $scriptPath "
cat" $scriptPath/readme "#使用绝对路径引用外部文件

Place the script in Listing 1 under the directory/opt/demo/scripts/auto-task and add the script to cron. The scheduled task runs the output as follows.

Current path is:/home/viscent

The script is located at:/opt/demo/scripts/auto-task
Command cannot find the problem

A script running under a scheduled task may have problems with the script parser that cannot find the relevant command. such as the Sqlplus command in an Oracle database, a script that executes under a scheduled task without special handling when invoking the command causes the script parser to not find the command, and an error message appears as follows:

Sqlplus:command not found

This is because scripts are executed by a non-logon shell when the script executes under a timed task, and the parent shell that executes the script is not the Oracle user's shell. Therefore, the. profile file for the Oracle user is not invoked at this time. The workaround is to add the following code at the beginning of the script:
Listing 2. Resolve unable to find an external command problem

Source/home/oracle/.profile

In other words, you can solve a problem that cannot be found by an external command by adding a statement from the source user's. profile file at the beginning of the script.
Script recurring problem

Another common problem with timed task scripts is the issue of running scripts repeatedly. For example, a script is set to run every 5 minutes. If the script cannot be run in 5 minutes at a time, the scheduled task service will still start a new process to execute the script. Then there are multiple processes running the same script. This can lead to scripting dysfunction. And it wastes system resources. There are usually two ways to avoid scripts running repeatedly. The first is to check the system for other processes running the script while the script is executing. If present, terminates the running of the current script. The second is that the script runs to check if there are other processes running the script in the system. If there is, then end that process (this method has a certain risk, use caution!) )。 Both of these methods need to check that the system already has a process running the current script at the beginning of the script, and then get the PID of the process if there is such a process. The sample code is shown in Listing 3 below.
Listing 3. Prevent scripts from running repeatedly method 1

#!/usr/bin/ksh
    
Main () {
selfpid= "$$"
scriptfile= "$"
    
typeset existingpid
' existingpid= ' Getexistingpids $selfPID "$scriptFile" '
    
if [!-Z ' $existingPid]; then
  echo "The script already running, exiting ... "
  exit-1
fi
    
doitstask
    
}
    
#获取除本身进程以外其它运行当前脚本的进程的 PID
getexistingpids () {
Selfpid= "$"
scriptfile= "$"
    
ps-ef | grep "/usr/bin/ksh ${scriptfile}" | grep-v "grep | awk" {if (\$2!= $selfP ID) Print \$2} "
}
    
Doitstask () {
echo" Task is now being executed ... "  #睡眠 20s, To simulate a script to perform a task that takes a long time to complete
}
    
main $*

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.