Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

What is the right way to write a bash script for cron job

I have written a bash script that is supposed to be executed by a cron job. But according to the cron output one of the lines in the bash script is not executing.

Here is my bash script

#! /bin/bash
/usr/bin/mysql</home/elijah/Desktop/covid-19-management-tool/cron_job/import.sql
./home/elijah/Desktop/covid-19-management-tool/cron_job/clear

This is the output I got from cron after execution

X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <20210205172001.F3C5FE221D@elijah-HP-255-G6-Notebook-PC>
Date: Fri,  5 Feb 2021 20:20:01 +0300 (EAT)

/home/elijah/Desktop/covid-19-management-tool/cron_job/dbs.sh: line 3: ./home/elijah/Desktop/covid-19-management-tool/cron_job/clear: No such file or directory

--F3C5FE221D.1612545602/elijah-HP-255-G6-Notebook-PC--

For line 3 in my bash script I want to run an executable called clear.

Someone please tell me how I should do it in the shell script so that cron can understand it.

question from:https://stackoverflow.com/questions/66068232/what-is-the-right-way-to-write-a-bash-script-for-cron-job

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Get rid of the . before /home (unless you have indeed the following directory structure):

/home/elijah/home/elijah/Desktop/covid-19-management-tool/cron_job/clear

The . makes this a relative path, while it's most likely meant to be absolute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...