Environment variables used by the script during crontab scheduling in Linux
Source: Internet
Author: User
When you use crontab to schedule a script, the environment variables used by the script are different from those used to run the script directly in the shell (because crontab does not know your shell, it simply sets environment variables such as HOME, LOGNAME, SHELL, and limited PATH, which may cause & ldquo; when you use crontab to schedule a script, the environment variables used by the script are different from those used to run the script directly in the shell (because crontab does not know your shell, only by setting environment variables such as HOME, LOGNAME, SHELL, and limited PATH), the "XXX command not find" error may occur or the running results may be inconsistent.
Solution 4:
1. set environment variables in the script, for example, in the cron_job.sh file:
#! /Bin/bash
Source $ HOME/. bash_profile
Some_other_cmd
Of course, you can also set the environment variables in a separate file such as/path/to/my_env.sh
$ Env | sort>/tmp/cmdLineEnv. out (note that the user used to run the script executes the command)
Then run 'cat/path/to/my_env.sh 'in the script to be run'
2. run the command using the absolute path in the script
You can avoid the issue that cannot be found by commands caused by PATH environment variables, but cannot avoid problems caused by other environment variables.
3. set the environment variable information used in the scheduling script in the crontab file, for example:
SHELL =/bin/bash
LANG = nb_NO.UTF-8
Lc_all= nb_NO.UTF-8
PATH =/sbin
0 5 **** sleep 5S & echo "yo"
Note the following when using this method:
Set at the top of the crontab file
This feature is only effective for certain cron implementations (for example, vixie-cron and GNU mcron of Ubuntu and Debin support this feature, whereas cronie of Archlinux and Redhat does not support this feature)
4. set environment variables in the scheduling statement, for example:
0 5 ***. $ HOME/. profile; sleep 5S & echo "yo"
Note the following when using this method:
"." Indicates the source Command, which cannot be ignored. of course, you can also directly replace "." with the "source" command.
Replace "$ HOME" with the actual full path.
The file for saving environment variables can be $ HOME/. profile, $ HOME/. bash_profile,/etc/profile, or a file specially created by you to store environment variables.
You can also directly set environment variables in the scheduling statement, for example:
0 5 *** SOME_ENV_VAR = some_value some_command some_parameters
Or write an encapsulation script to encapsulate the command for setting environment variables, such as cron-wrapper.
#! /Bin/bash
[-R $ HOME/. bashrc] & amp;. $ HOME/. bashrc
[-R $ HOME/. profile] & amp;. $ HOME/. profile
Exec "$ @"
Then, in the scheduled task scheduling statement
* *** 1-5 ~ /Scripts/cron-wrapper ~ /Scripts/myscript. sh
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