-Crontab environment variables caused by crontab execution of the reverse CT script

Source: Internet
Author: User

There is a crash CT script to remotely restart the processes on the target server. Everything is OK when it is executed separately under shell, but it cannot be executed when it is added to the cron task. Some code is as follows:

Secondary CT Script: (/data/home/restart_secondary CT)

#! /Usr/bin/CT

Spawn ssh-p xxx root@xxx.xxx.xxx.xxx

# Keep CT "* Root's password *"
Secondary CT "* Tencent :*"

Send "Sh some remote scripts"
Send "Exit/N"

Ct eof exit 0

Crontab task:

*/5 */data/monitor/new_itil_dicts/syn_tcms_data/run_syn.php/tmp/test. log 2>/dev/null

There is no problem in executing everything in shell alone, but it cannot be put into cron. Start to locate the problem. The entire process is as follows:

1. First, the task of modifying crontab is incorrectly output to an execution file.

*/5 */data/monitor/new_itil_dicts/syn_tcms_data/run_syn.php/tmp/test. log 2> & 1

2. view the error output in the/tmp/test. log file. The prompt is as follows:

I cannot find the command SSH, which makes me a bit confused. Why can't I find the SSH command (I installed it in the/usr/local/bin directory, the echo $ path/usr/local/bin directory is clearly in the path. At this time, it is assumed that the environment variables used by cron are different from those used by shell, So Google, it is found that cron contains the following content in/etc/crontab:

Shell =/bin/sh
Path =/usr/bin:/usr/sbin:/bin:/usr/lib/news/bin
Mailto = root
#
# Check scripts in cron. Hourly, cron. daily, cron. weekly, and cron. Monthly
#

Note that the PATH variable here finds that the SSH installation directory is not above this. Here we will know the problem caused by the use of different path variables in cron and shell. So the solution can be:

1. Modify the PATH variable of/etc/crontab to include commands used in audit CT, such as SSH

Shell =/bin/sh
Path =/usr/bin:/usr/sbin:/bin:/usr/lib/news/bin:/usr/local/bin
Mailto = root

2. the full path of the command used by the keep CT script is as follows,

#! /Usr/bin/CT

Spawn/usr/local/bin/ssh-p xxx root@xxx.xxx.xxx.xxx

# Keep CT "* Root's password *"
Secondary CT "* Tencent :*"

Send "Sh some remote scripts"
Send "Exit/N"

Ct eof exit 0

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.