Why does my bash script work in a terminal, but not when using crontab?
The user name precedes the command to run in the system crontab. Use
0 * * * * root /bin/bash /home/henry/yupdate.sh > /dev/null 2>&1
instead.
Cron doesn't accept bash syntax
Thanks to the help of the people here I found my issue was not syntax but rather the use of sh over bash. This was fixed by setting the crontab this way so future users can see:
# m h dom mon dow command
SHELL=/bin/bash
@reboot cd /home/ubuntu/waste-cloud-computing/probe && ./probe.sh >> /var/log/somelogfile.log 2>&1
The key points are the SHELL variable being set and the ./ before running the script.
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 * * * * *
.
Bash script commands not working in cron
Try to provide full path to iptables e.g.
$ which iptables
/sbin/iptables
and than modify your script like that:\
#!/bin/bash
#blah blah run some commands to get the IP
/sbin/iptables -A INPUT -s $p -j REJECT --reject-with icmp-host-prohibited
echo "BANNED $p FOR $COUNT ATTEMPTS" |wall
Related Topics
Compare Two Files of Different Columns and Print Different Columns
Bash - Ignore Hidden Files and Empty Source Directory When Copying
"The Launch Timed Out and Was Terminated" Error with Bumblebee on Linux
Put Graphics on The Screen Without /Dev/Fb0
How to Find Libstdc++.So.6: That Contain Glibcxx_3.4.19 for Rhel 6
Run Hydra (Mpiexec) Locally Gives Strange Ssh Error
Is There Any Way for Ioctl() in Linux to Specify Submission Queue Id for a Nvme Io Request
Setting an Acpi Field in Linux
How to Delay Pipe Netcat to Connect on First Input
Awk: Sum Up Column Values Across Multiple Files with Identical Column Layout