4

I have a Shell script like below

 echo "Hello World"

The script is located in /root/scripts/ folder as test.sh

I also created a cron job like below

  0-59 * * * *  ./scripts/test.sh

Now the cron job is not printing the content in test.sh every minute.

Let me know whether I have given a wrong directory or I have any other problem in my code.

eLRuLL
  • 18,488
  • 9
  • 73
  • 99
user1093513
  • 127
  • 2
  • 4
  • 12

4 Answers4

7

I would

  1. Be explicit wrt. your directory to execute from e.g. /root/scripts/test.sh. I don't know what cron would regard as the current directory
  2. Redirect stdout to a log file e.g. ...test.sh > /tmp/cron.log (you would likely want to redirect stderr at some stage too using 2>&1). Otherwise you're not going to see the output. It gets mailed to the cronjob owner
  3. Make sure you've given execution permission to your bash script (chmod +x /root/scripts/test.sh)
  4. Be explicit which script executable will execute your shell script. It's good practise to have a script invocation at the top e.g. #!/bin/sh or similar

Getting stuff to run under cron is notoriously tricky. Cron jobs run with a massively cut-down environment. It's instructive to print out the environment available to your script (use env) and compare/contrast with what you have available from your interactive shell.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
4

A common problem encountered by new unix users when using cron to launch scripts is that their cronjobs do not have access to the same environment as their user shell. So a script executed in a user's shell works perfectly, but fails when launched from cron. The number one culprit in this case is that your shell's $PATH environment variable differs from that of cronjob's shell.

To test run '/bin/echo $PATH > myShellPath'. In cron run '/bin/echo $PATH > myCronPath'. Compare myShellPath with myCronPath.

In the example provided the script executes echo, which on my CENTOS6 system is found under /bin. The echo command is found by the shell by searching for the 'echo' file in one of the directories listed in the $PATH environment variable. If your cronjob's $PATH environment variable does not include /bin, the echo command will fail.

If this is the problem, the simplest solution is to explicitly set the path to 'echo' in your script. To view your system's full path for echo, type 'which echo' at your shell.

Example:

[countChocula@bozo ~]$ which echo
/bin/echo

In this case you would edit the script and replace 'echo' with '/bin/echo'. Better still, set the $PATH variable in your script to include the directory in which echo resides.

For further information, research "what shell am i running and how are my environment variables set?", "what shell is cron using and how do i set cron environment variables?", "how to view my cronjob's environment variables?"

mcchiz
  • 742
  • 6
  • 11
2

You Should use

* * * * * /path/to/scriptFile

for run script in every minute

and check owner of your script folder if owner haven't permssion to execute script give permission or change owner who have permission

mat
  • 1,367
  • 1
  • 13
  • 18
Vaibhav Jain
  • 493
  • 2
  • 7
  • 13
  • Hi I wrote a scripts that works good if I use it manually with out cron but on cron it doesnt work. Here is my cron: * * * * * sudo /opt/modulo/log/check.sh if you can help me I will be happy – Tomer May 14 '17 at 12:17
0

When you add the file in the cron not understand './scripts/test.sh'. So you should add cron as follows:

  0-59 * * * *  bash /root/scripts/test.sh

And you add permission for file as:

chmod a+x /root/scripts/test.sh

When script run that okey.

Richbest
  • 151
  • 10