1

I read the other related topics but they didn't help me.

I have a shell script which checks if my python script is not running,it will run it. Otherwise it will just skip and do nothing.

It totally works when I use: bash myshellscrip.sh And I get the result that I want which is doing some tasks and sending emails to some correspondents. However, when I try to run this particular shell script on crontab, it doesn't send out the emails and doesn't do the other tasks. I tried the following on crontab and none of them worked.

    * * * * * /bin/bash /path/to/my/script/myshellscrip.sh 
    * * * * * /bin/bash /path/to/my/script/myshellscrip.sh >> /some/other/path/output.txt

When I save the changes into 'output.txt' file, it creates the file but it doesn't send the emails or doing other tasks.

I also tried the option of reboot because I need this program to run at start up too, and this didn't work:

    @reboot /bin/bash /path/to/my/script/myshellscrip.sh

Does anyone know how to fix it?

EDIT:

As I was checking with the simplest shell scrip like:

    #!/bin/sh
    /usr/bin/python /home/pi/DCA/code.py

My crontab wouldn't have any output in my output.txt file although my code.py have something printing out, too.

However, when I use a very simple python code for example only a 'print' statement it will run and save the output into output.txt.

Luna2
  • 21
  • 7
  • 1
    Post the contents of the script – Sunitha Jul 19 '18 at 17:29
  • 2
    you are likely using commands that require your environment or an interactive shell to work correctly. [see here](https://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with) – jeremysprofile Jul 19 '18 at 17:30
  • 1
    Please go through the debugging steps on the [cron tag wiki](https://stackoverflow.com/tags/cron/info) and update your post with the error log and other results – that other guy Jul 19 '18 at 17:41
  • Execute it with `bash -x` instead; and also redirect the stderr to output.txt. The way you are doing it, you are dropping the error messages. – user1934428 Jul 20 '18 at 04:17
  • @Sunitha. The simplest content of the scrip is : #!/bin/sh /usr/bin/python /home/pi/DCA/code.py – Luna2 Jul 23 '18 at 17:06
  • @thatotherguy there is no error – Luna2 Jul 23 '18 at 17:15
  • @user1934428 I did it with -x, and there is no error. – Luna2 Jul 23 '18 at 17:16
  • It *will* show output. If you're not getting any it's because you're not capturing stderr. You can use `bash -x yourscript > /tmp/log 2>&1` for that – that other guy Jul 23 '18 at 17:18
  • @thatotherguy I was doing bash -x yourscript >> /tmp/log 2>&1 with '>>' and it wasn't showing me anything. Thank you so much for the help. I found where it has the error. – Luna2 Jul 23 '18 at 17:46
  • https://stackoverflow.com/a/49296678/1135424 may help testing – nbari Jul 23 '18 at 18:53
  • @Luna2 : Well, you wouldn't expect that `-x` would show an *error* message which wasn't visible before, would you? However, you see what commands are actually executed (for instance, how the variables get substituted), and this might lead you to the bug in your program. – user1934428 Jul 24 '18 at 06:09

3 Answers3

2

Seems like your shell script crashes / stops before it can do something (possibly due to the environment being different or permission issues). You can check /var/log/syslog to find out.

You could try removing /bin/bash, I don't think that's necessary?

RRT
  • 156
  • 2
  • 3
0

Run the cron job in debug mode. for that, Add -x to the bash command on the cronjob and save their output in the file.

bash -x /path/to/script.sh >> /path/to/the/output.txt

You can find the problem.

vahit
  • 69
  • 4
  • The file will get created but there it is an empty file and has nothing inside. – Luna2 Jul 23 '18 at 17:05
  • If your script redirect error messages to stderr, use as this `bash -x /path/to/script.sh >> /path/to/the/output.txt 2>&1` – vahit Dec 13 '19 at 22:50
0

Apparently crontab was running my script several times. So I tried to use different locking mechanisms to put a lock around my scrip but only using flock worked for me. In my crontab I added this line:

* * * * * /usr/bin/flock -n /tmp/ms.lockfile /bin/bash /path/to/my/script/myShellScript.sh
Luna2
  • 21
  • 7