Crontab non-effective solution of ENV environment variable caused by Linux

Source: Internet
Author: User

Crontab is a scheduled task in Linux, but the task plan before the persecution using the ENV environment variable is not working, let's look at how to solve the crontab problem caused by the ENV environment variable.


A friend said that he wrote a script to do the scheduled task to back up the database, but the backup is not successful, manual execution of the script is not a problem, and in the/var/log/cron log to see the successful execution, but is not see the backup database files.

With SSH permissions, after the login, after reading, and what he said, the script executes successfully, but no backup is generated.

However, check the email sent to root and see the problem:
Return-path: <>
X-original-to:root (Cron Daemon)
Subject:cron <root@test> sh/data/cron/
Content-type:text/plain; Charset=utf-8
X-cron-env: <lang=en_us. Utf-8>
X-cron-env: <SHELL=/bin/sh>
X-cron-env: <HOME=/root>
X-cron-env: <PATH=/usr/bin:/bin>
X-cron-env: <LOGNAME=root>
X-cron-env: <USER=root>
Date:tue, 1 APR 2014 10:58:01-0400 (EDT)

/data/cron/ 27:mysql:command not found

From the above can be seen, said the MySQL command did not find, and then executed a command:

[Root@test data]# Echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/usr/local/mysql/ Bin

This is where the problem is, the execution environment variables in crontab are inconsistent with the environment variables set by the root user, and the difference can be seen from the above two:
Environment variables in crontab: x-cron-env: <PATH=/usr/bin:/bin>

environment variable for root user:
path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/usr/local/ Mysql/bin

Now that we know that the MySQL command is placed under/usr/local/mysql/bin, and Crontab's path variable does not, there is a problem mysql:command not found.

At the same time, we can create a new script that validates the path variable in crontab:
[Root@test data]# VI

Echo $PATH

# after saving, add to Crontab

[Root@test data]# Crontab-e
*/1 * * * */data/

After 1 minutes, you can see:


Know the problem, that is good to solve, there are two kinds:

1. In the backup script, set it manually, for example:


. /etc/profile

.... The rest ...
Here needs ". /etc/profile ", is because the friend MySQL compiles itself, adds the MySQL path all to here:
Path= $PATH: $HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin
Export PATH

2. Directly set the user's crontab, add path/usr/local/mysql/bin, for example:
[Root@test data]# Crontab-e
path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/usr/local/ Mysql/bin:/root/bin

0 */12 * * * sh/data/cron/

After you save it, you can back it up normally.

You can also learn about a file/etc/crontab:
[root@test data]# cat/etc/crontab
Path=/sbin:/bin:/usr/sbin :/usr/bin
# for details, man 4 crontabs
# Example of Job Defin Ition:
#.----------------minute (0-59)
# | .-------------Hour (0-23)
# |  | .--------- -Day of Month (1-31)
# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...
# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# |&NB Sp. |  |  |  |
# *  *  *  *  * user-name command to be executed

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: 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.