Bash script runs manually, but fails on crontab
The problem is probably that your $PATH is different in the manual environment from that under which crontab runs. Hence, which
can't find your executables. To fix this, first print your path in the manual environment (echo $PATH
), and then manually set up PATH at the top of the script you run in crontab. Or just refer to the programs by their full path.
Edit: Add this near the top of your script, before all the which
calls:
export PATH="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin"
shell script not running via crontab, runs fine manually
Try specifying the full path to the jar file:
/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o
cronjob does not execute a script that works fine standalone
As seen in comments, the problem is that you are not defining what program should be used to execute the script. Take into account that a cronjob is executed in a tiny environment; there, not much can be assumed. This is why we define full paths, etc.
So you need to say something like:
1 * * * * /bin/sh /var/www/html/dbsync/dbsync.sh /var/www/html/dbsync
# ^^^^^^^
/bin/sh
being the binary you want to use to execute the script.
Otherwise, you can set execution permissions to the script and add a shell-script header telling it what interpreter to use:
#!/bin/sh
If you do this, adding the path of the binary is not necessary.
From Troubleshooting common issues with cron jobs:
Using relative paths. If your cron job is executing a script of some
kind, you must be sure to use only absolute paths inside that script.
For example, if your script is located at /path/to/script.phpand
you're trying to open a file called file.php in the same directory,
you cannot use a relative path such as fopen(file.php). The file must
be called from its absolute path, like this: fopen(/path/to/file.php).
This is because cron jobs do not necessarily run from the directory in
which the script is located, so all paths must be called specifically.
Also, I understand you want to run this every minute. If so, 1 * * * *
won't do. Intead, it will run at every 1st minute past every hour. So if you want to run it every minute, say * * * * *
.
Shell script runs manually but not executed through cron job
I have found the solution. That I have removed the user from crontab entry & it worked.
Original crontab entry:
[root@testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
After modification:
[root@testVM-dev log]# crontab -l
*/1 * * * * /path_to_dir/log_cleanup.sh
Now I got confused why the user entry in cron job didn't work? Can someone explain?
crontab not running shell script
Thanks for everyone that took the time to answer. So the problem was not with crontab at all but was actually an issue with Python virtual environments. Using source activate
was not working with crontab. The solution was to modify the shell script so that it directly uses the Python in the specified virtual environment. For example..
#! /bin/bash
cd /home/pi/scripts
/home/pi/berryconda3/envs/myEnv/bin/python pyscript.py
Also I had logging done within Python but because it crashed early in the script they were never written so I incorrectly assumed the crontab was not executing correctly. I have since discovered that it is a very good idea when working with crontab to log what's going on directly from cron like this...
* * * * * /home/pi/bash/myshell.sh >> /home/pi/logs/cronlog 2>&1
This way I could actually see what was going wrong (i.e. Python was throwing ModuleNotFoundError
).
There's a much better answer on this here that is worth looking at. I'll mark this question as a duplicate.
Related Topics
Simpler Way to Repeatedly Read Lines and Invoke a Program
Rpm Spec to Require Specific Rhel Release
Fortran: How to Get The Node Name of a Cluster
Perl Script to Capture Stderr and Stdout of Command Executed in Back-Quotes
Creating Filename_$(Date %Y-%M-%D) from Systemd Bash Inline Script
How to Find/Cut for Only The Filename from an Output of Ls -Lrt in Perl
How to Generate Multiple Ssh Public Key and Configure Those on Windows Machine from Gitbash
How to Efficiently Get 10% of Random Lines Out of The Large File in Linux
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize
A Wrong Size of "Len" Calculated by $ - Symbol with Fasm Equ
How to Rename Files in Zip Archive Without Extracting and Recompressing Them
How to Create a File of Size More Than 2Gb in Linux/Unix
Mongodb Service Fails After Reboot