当前位置:网站首页>Crontab pit stepping record: manual script execution is normal, but crontab timed script execution is abnormal

Crontab pit stepping record: manual script execution is normal, but crontab timed script execution is abnormal

2022-06-21 15:00:00 I've been to places that span an hour

One 、 introduction

Recently, I met an interesting problem in my work , The background is as follows :

I want to Linux Add a timed task to the environment , Every morning 3 Point to perform , Used to send some data files to the docking party

I am here $HOME/shell I wrote a script , then crontab Set up an execution plan , Do it manually The script , Found no problem on the happy off work .

Who knows, the next day the docking party contacted me and said , In the morning 3 I didn't get the document .

What's the situation ? I didn't check it out crontab Is the configuration of ?

I went over my script log again , Surprised to find an incredible problem :

crontab The result of execution is obviously different from that of manual execution

Why is that ?

Two 、 Explore

I checked it carefully crontab Configuration of , There are no configuration errors ; Again Do it manually The script was created , And I didn't find any problems . In the end, I can only ask for help from script logs , Finally found some clues :

1. In my script , Using a self-made executable program $HOME/bin/PushFile Program to send files , And this executable is in Do it manually When , It can be found , But in crontab When it's automatic , But I can't find it , So there is a problem of script running abnormally , Causes the file to fail to send

2. So why crontab Automatic execution can't find executable program ? because $HOME/bin There are no variables in the environment $PATH in . When our users log in , My own .profile The file added itself PATH="$HOME/bin:$PATH" The environment variable definition of , and crontab did not

in general , Namely :

because crontab Missing initialization of some environment variables , Some executable programs that are invoked in the script are $PATH Not found in , So the script execution is abnormal

therefore , That's what led to this crontab Automatic execution exception , But there is no problem with manual execution .

3、 ... and 、 solve

So how to solve this problem , There are two ways :

1. modify crontab Task allocation

0 3 * * * (. /home/wangying/.profile; sh /home/wangying/SendFile.sh)

2. modify SendFile.sh Script , Put it PushFile Call to full path /home/wangying/bin/PushFile

All in all , It is to let the script find the location of the executable program in the script , Either initialize the environment variables , Or give the full path of the executable program . such , We can solve the problems in the introduction ~~~

I hope this blog can help you :)

原网站

版权声明
本文为[I've been to places that span an hour]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221328568086.html