Bash script not running via cron and sh , but runs fine with ./filename.sh - bash

I have a code which crontab is not able to run
i am using ./filename.sh to run it. when i do this manually this runs fine
but via cron when i try
*/5 * * * * . /home/ubuntu/filename.sh >> /filename.sh
This doesn't work
for ((i=0; i<retries; i++)); do
curl -1 --cipher ALL --connect-timeout 90 -T $zip_name ftps://ftp.box.com/Backup/$zip_name --user admin:pas [[ $? -eq 0 ]] && break
echo "something went wrong, let's wait 6 seconds and retry"
sleep 6
done
[[ $retries -eq i ]] && { echo "This email is being sent as a notifer of Failure, Support" | mail -s "Dump Failed" "asdfas4#gmail.com" ; exit 1; }
Also when i run this using sh
it says Syntax error: Bad for loop variable

Presumably, the shell of your cron is dash (not bash). In Ubuntu (and derivatives) sh is just a symlink for dash.
You can:
Add a shebang at the top of of your script -- #!/bin/bash (or #!/usr/bin/env bash), recommended approach
Run the script as an argument to bash: /bin/bash /path/to/script.sh, moderately recommended
Set SHELL=/bin/bash in your crontab (not recommended), or even you can set the SHELL as bash for the run of any single command but again use the shebang approach
Also, always try to use absolute path to any file in crontab as cron runs with a modified PATH.
Now, even in bash, your for construct's syntax is incorrect, you need the arithmetic operator (()), not subshell (()):
for ((i=0; i<retries; i++)); do ...; done

If you source the script by . operator, then shebang #!/bin/bash in the script is ignored. The script is executed in the shell of your cron, which is not bash and it doesn't support for loop you are using.
Add shebang in your script and remove . from cron file:
*/5 * * * * /home/ubuntu/filename.sh >> /filename.sh
or just edit cron file in the following way:
*/5 * * * * /bin/bash /home/ubuntu/filename.sh >> /filename.sh

Don't use . when you are giving full path to the script, try running below command:
*/5 * * * * /home/ubuntu/filename.sh
It should work, Let me know in case of more details.

Related

How to use functions within crontab

My crontab has several similar calls where a script is called with a flock file, timeout, and output / error logs. I'd like to put this logic into a shared function that I just pass the script path and timeout length to. Is there any way to define a function within the crontab that can be used by all entries?
The best workaround I've found so far is defining my function in my .bashrc file, and wrapping every cron command with bash -ic "..." to make them run in an interactive shell, but this seems overkill, and means my crontab's functionality is linked to my .bashrc file. Is there no better way to use functions within the crontab?
Testing on Ubuntu Server 20.04 LTS
---- Edit ----
Per the comments, here's an example entry in my crontab:
0 */4 * * * IFS=; output=$(flock -n /home/me/my_script.lock timeout 3600 python3 /home/me/my_script.py 2>&1 || if [ $? -eq 124 ]; then echo "`date '+\%s'`: Killed due to timeout"; fi); if [ "$output" ]; then echo $output; fi >> /home/me/logs/my_script.log 2>&1
...and the corresponding test function I put in my .bashrc file
test() {
IFS=;
output=$(flock -n test.lock timeout 300 python3 test.py 2>&1 || if [ $? -eq 124 ]; then echo "`date '+\%s'`: Killed due to timeout"; fi);
if [ "$output" ]
then
echo $output
fi
}
...the function of course being hard-coded just for testing, if used I would add parameters for the script path and timeout length per OP.
Hopefully this better explains the desire to use a function, as the whole IFS, flock, timeout, and 'killed by' pieces are all reused for each line (of which there are multiple dozen). If there's any better solution catered to this need, all suggestions welcome, otherwise the suggestion to just call my function as a separate bash script sounds appropriate.
Put the function in a file and you can do
0 * * * * . /path/to/file; func_name arg1 arg2
Be aware thaat the default shell for the crontab is /bin/sh. If your function relies on bash features, set SHELL = /bin/bash in the crontab. See man 5 crontab

run script as root bash

I have the below bash script that I'm trying to schedule as a cron job.
I have it in /etc/cron.d/cronjob
*/1 * * * * root /home/area/reboot.sh
But it's not working...
if I run the script from the command line using
sudo /home/area/reboot.sh
if works fine
below is the script
#!/bin/bash
if [[ `awk '{print $0/60;}' /proc/uptime | cut -d . -f1` -gt 10 ]];then
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger
fi
Just use sudo crontab -e to edit the crontab
Also use
*/1 * * * * /home/area/reboot.sh
instead of
*/1 * * * * root /home/area/reboot.sh
Set path at the beginning of your script:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
At this task there are some misleading infos.
First, for the system cron tables (/etc/crontab, /etc/cron.d/*) a username is needed. The job is executed under the environment or that user. This is a difference to the usual user-crontab. So This is correct:
*/1 * * * * root /home/area/reboot.sh
^^^^
username
About */1: It means: Every minute, that can be divided by 1 without remainder. So it is the same as *.
An answer to your question:
I have no idea, what is going wrong. Setup of PATH= or SHELL= may be a solution, or not. Anyway, setup MAILTO= and cron will send you a mail on errors. Read man 5 crontab. If it is not available at your system, google it.
Make sure you have execute permission for the cron file.
chmod +x /home/area/reboot.sh
Also try adding the sh in front of the script. So in crontab -e
*/1 * * * * /bin/sh /home/area/reboot.sh
fixed this with the below
echo 1 | sudo tee /proc/sys/kernel/sysrq
echo b | sudo tee /proc/sysrq-trigger

How to make bash script execute command at regular intervals

I'm working on a bash script that will run for appoximately 30 minutes at a time. I've got it running stable as far as that part goes. I've been looking for a way to make it fire certain commands at inervals of every 3 minutes while running. I've not had any luck, so I turn to those of you that may know more about bash than I.
Any suggestions?
Here is what I have in mind of doing.
START=$(date +%s);
while read LINE <&3; do
END=$(date +%s);
if [[ $(($END-$START)) > 180 || $(($END-$START)) == 180 ]]
then
$START=$(date +%s);
run command
fi
done
Add a cron job to make it run every 3 minutes.
*/3 * * * * /path/to/script
What about the watch command?? (https://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever)
(Second answer on here: Run command every second)
You can run a loop in the background:
{ while /bin/true; do some_command; sleep 180; done; } &
loop_pid=$!
Then before the main script exits, kill the background loop:
kill $loop_pid
You can also call the same script from same script.
$ cat script.sh
#!/bin/bash
# commands
# commands
sleep 1800
sh $0

pidof in cron not finding process?

I'd like to restart my daemon if it's not running (crashed etc). inittab is not applicable for various reasons. This snippet works fine in bash but not from cron as it keeps starting multiple processes:
*/1 * * * * /bin/bash if [ ! $(pidof vzlogger) ]; then sudo vzlogger -d; fi;
Is the subshell "eating" the exit code of pidof? The alternative
*/1 * * * * /bin/bash if [ -z "$(pidof vzlogger)" ]; then sudo vzlogger -d; fi;
has the same problem- multiple processes?
The way to run Bash commands is not bash commands but bash -c 'commands'.
*/1 * * * * /bin/bash -c 'pidof vzlogger >/dev/null || sudo vzlogger -d'
Of course, the /1 is redundant, and you don't need Bash for any of this.
* * * * * pidof vzlogger >/dev/null || sudo vzlogger -d
The if test wasn't incorrect per se, but it can very often be avoided. So, for example, pidof fortunately returns error if it did not find a PID, and success otherwise; so you can use the shortcut syntax. (Most properly maintained Unix tools have this feature.) Because the PID is no longer captured in a (superfluous) process substitution, we redirect the output from pidof to /dev/null (because otherwise you will receive email from the cron daemon every time it succeeds and generates output).
/bin/bash if will search for a file named if in the current directory (which for a cron job is your home directory), and attempt to execute it as a Bash script.
You should have received an email from the cron daemon with an error message:
bash: if: No such file or directory

How to create a cron job using Bash automatically without the interactive editor?

Does crontab have an argument for creating cron jobs without using the editor (crontab -e)? If so, what would be the code to create a cron job from a Bash script?
You can add to the crontab as follows:
#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron
Cron line explaination
* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Source nixCraft.
You may be able to do it on-the-fly
crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -
crontab -l lists the current crontab jobs, cat prints it, echo prints the new command and crontab - adds all the printed stuff into the crontab file. You can see the effect by doing a new crontab -l.
This shorter one requires no temporary file, it is immune to multiple insertions, and it lets you change the schedule of an existing entry.
Say you have these:
croncmd="/home/me/myfunction myargs > /home/me/myfunction.log 2>&1"
cronjob="0 */15 * * * $croncmd"
To add it to the crontab, with no duplication:
( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
To remove it from the crontab whatever its current schedule:
( crontab -l | grep -v -F "$croncmd" ) | crontab -
Notes:
grep -F matches the string literally, as we do not want to interpret it as a regular expression
We also ignore the time scheduling and only look for the command. This way; the schedule can be changed without the risk of adding a new line to the crontab
Thanks everybody for your help. Piecing together what I found here and elsewhere I came up with this:
The Code
command="php $INSTALL/indefero/scripts/gitcron.php"
job="0 0 * * 0 $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -
I couldn't figure out how to eliminate the need for the two variables without repeating myself.
command is obviously the command I want to schedule. job takes $command and adds the scheduling data. I needed both variables separately in the line of code that does the work.
Details
Credit to duckyflip, I use this little redirect thingy (<(*command*)) to turn the output of crontab -l into input for the fgrep command.
fgrep then filters out any matches of $command (-v option), case-insensitive (-i option).
Again, the little redirect thingy (<(*command*)) is used to turn the result back into input for the cat command.
The cat command also receives echo "$job" (self explanatory), again, through use of the redirect thingy (<(*command*)).
So the filtered output from crontab -l and the simple echo "$job", combined, are piped ('|') over to crontab - to finally be written.
And they all lived happily ever after!
In a nutshell:
This line of code filters out any cron jobs that match the command, then writes out the remaining cron jobs with the new one, effectively acting like an "add" or "update" function.
To use this, all you have to do is swap out the values for the command and job variables.
EDIT (fixed overwriting):
cat <(crontab -l) <(echo "1 2 3 4 5 scripty.sh") | crontab -
There have been a lot of good answers around the use of crontab, but no mention of a simpler method, such as using cron.
Using cron would take advantage of system files and directories located at /etc/crontab, /etc/cron.daily,weekly,hourly or /etc/cron.d/:
cat > /etc/cron.d/<job> << EOF
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
01 * * * * <user> <command>
EOF
In this above example, we created a file in /etc/cron.d/, provided the environment variables for the command to execute successfully, and provided the user for the command, and the command itself. This file should not be executable and the name should only contain alpha-numeric and hyphens (more details below).
To give a thorough answer though, let's look at the differences between crontab vs cron/crond:
crontab -- maintain tables for driving cron for individual users
For those who want to run the job in the context of their user on the system, using crontab may make perfect sense.
cron -- daemon to execute scheduled commands
For those who use configuration management or want to manage jobs for other users, in which case we should use cron.
A quick excerpt from the manpages gives you a few examples of what to and not to do:
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. The files under /etc/cron.d do not need to be executable, while the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly do, as they are run by run-parts (see run-parts(8) for more information).
Source: http://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
Managing crons in this manner is easier and more scalable from a system perspective, but will not always be the best solution.
So, in Debian, Ubuntu, and many similar Debian based distros...
There is a cron task concatenation mechanism that takes a config file, bundles them up and adds them to your cron service running.
You can put a file under the /etc/cron.d/somefilename where somefilename is whatever you want.
sudo echo "0,15,30,45 * * * * ntpdate -u time.nist.gov" >> /etc/cron.d/vmclocksync
Let's disassemble this:
sudo - because you need elevated privileges to change cron configs under the /etc directory
echo - a vehicle to create output on std out. printf, cat... would work as well
" - use a doublequote at the beginning of your string, you're a professional
0,15,30,45 * * * * - the standard cron run schedule, this one runs every 15 minutes
ntpdate -u time.nist.gov - the actual command I want to run
" - because my first double quotes needs a buddy to close the line being output
>> - the double redirect appends instead of overwrites*
/etc/cron.d/vmclocksync - vmclocksync is the filename I've chosen, it goes in /etc/cron.d/
* if we used the > redirect, we could guarantee we only had one task entry. But, we would be at risk of blowing away any other rules in an existing file. You can decide for yourself if possible destruction with > is right or possible duplicates with >> are for you. Alternatively, you could do something convoluted or involved to check if the file name exists, if there is anything in it, and whether you are adding any kind of duplicate-- but, I have stuff to do and I can't do that for you right now.
For a nice quick and dirty creation/replacement of a crontab from with a BASH script, I used this notation:
crontab <<EOF
00 09 * * 1-5 echo hello
EOF
Chances are you are automating this, and you don't want a single job added twice.
In that case use:
__cron="1 2 3 4 5 /root/bin/backup.sh"
cat <(crontab -l) |grep -v "${__cron}" <(echo "${__cron}")
This only works if you're using BASH. I'm not aware of the correct DASH (sh) syntax.
Update: This doesn't work if the user doesn't have a crontab yet. A more reliable way would be:
(crontab -l ; echo "1 2 3 4 5 /root/bin/backup.sh") | sort - | uniq - | crontab -
Alternatively, if your distro supports it, you could also use a separate file:
echo "1 2 3 4 5 /root/bin/backup.sh" |sudo tee /etc/crond.d/backup
Found those in another SO question.
echo "0 * * * * docker system prune --force >/dev/null 2>&1" | sudo tee /etc/cron.daily/dockerprune
A variant which only edits crontab if the desired string is not found there:
CMD="/sbin/modprobe fcpci"
JOB="#reboot $CMD"
TMPC="mycron"
grep "$CMD" -q <(crontab -l) || (crontab -l>"$TMPC"; echo "$JOB">>"$TMPC"; crontab "$TMPC")
(2>/dev/null crontab -l ; echo "0 3 * * * /usr/local/bin/certbot-auto renew") | crontab -
cat <(crontab -l 2>/dev/null) <(echo "0 3 * * * /usr/local/bin/certbot-auto renew") | crontab -
#write out current crontab
crontab -l > mycron 2>/dev/null
#echo new cron into cron file
echo "0 3 * * * /usr/local/bin/certbot-auto renew" >> mycron
#install new cron file
crontab mycron
rm mycron
If you're using the Vixie Cron, e.g. on most Linux distributions, you can just put a file in /etc/cron.d with the individual cronjob.
This only works for root of course. If your system supports this you should see several examples in there. (Note the username included in the line, in the same syntax as the old /etc/crontab)
It's a sad misfeature in cron that there is no way to handle this as a regular user, and that so many cron implementations have no way at all to handle this.
My preferred solution to this would be this:
(crontab -l | grep . ; echo -e "0 4 * * * myscript\n") | crontab -
This will make sure you are handling the blank new line at the bottom correctly. To avoid issues with crontab you should usually end the crontab file with a blank new line. And the script above makes sure it first removes any blank lines with the "grep ." part, and then add in a new blank line at the end with the "\n" in the end of the script. This will also prevent getting a blank line above your new command if your existing crontab file ends with a blank line.
Bash script for adding cron job without the interactive editor.
Below code helps to add a cronjob using linux files.
#!/bin/bash
cron_path=/var/spool/cron/crontabs/root
#cron job to run every 10 min.
echo "*/10 * * * * command to be executed" >> $cron_path
#cron job to run every 1 hour.
echo "0 */1 * * * command to be executed" >> $cron_path
Here is a bash function for adding a command to crontab without duplication
function addtocrontab () {
local frequency=$1
local command=$2
local job="$frequency $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -
}
addtocrontab "0 0 1 * *" "echo hello"
CRON="1 2 3 4 5 /root/bin/backup.sh"
cat < (crontab -l) |grep -v "${CRON}" < (echo "${CRON}")
add -w parameter to grep exact command, without -w parameter adding the cronjob "testing" cause deletion of cron job "testing123"
script function to add/remove cronjobs. no duplication entries :
cronjob_editor () {
# usage: cronjob_editor '<interval>' '<command>' <add|remove>
if [[ -z "$1" ]] ;then printf " no interval specified\n" ;fi
if [[ -z "$2" ]] ;then printf " no command specified\n" ;fi
if [[ -z "$3" ]] ;then printf " no action specified\n" ;fi
if [[ "$3" == add ]] ;then
# add cronjob, no duplication:
( crontab -l | grep -v -F -w "$2" ; echo "$1 $2" ) | crontab -
elif [[ "$3" == remove ]] ;then
# remove cronjob:
( crontab -l | grep -v -F -w "$2" ) | crontab -
fi
}
cronjob_editor "$1" "$2" "$3"
tested :
$ ./cronjob_editor.sh '*/10 * * * *' 'echo "this is a test" > export_file' add
$ crontab -l
$ */10 * * * * echo "this is a test" > export_file
No, there is no option in crontab to modify the cron files.
You have to: take the current cron file (crontab -l > newfile), change it and put the new file in place (crontab newfile).
If you are familiar with perl, you can use this module Config::Crontab.
LLP, Andrea
script function to add cronjobs. check duplicate entries,useable expressions * > "
cronjob_creator () {
# usage: cronjob_creator '<interval>' '<command>'
if [[ -z $1 ]] ;then
printf " no interval specified\n"
elif [[ -z $2 ]] ;then
printf " no command specified\n"
else
CRONIN="/tmp/cti_tmp"
crontab -l | grep -vw "$1 $2" > "$CRONIN"
echo "$1 $2" >> $CRONIN
crontab "$CRONIN"
rm $CRONIN
fi
}
tested :
$ ./cronjob_creator.sh '*/10 * * * *' 'echo "this is a test" > export_file'
$ crontab -l
$ */10 * * * * echo "this is a test" > export_file
source : my brain ;)
Say you're logged in as the user "ubuntu", but you want to add a job to a different user's crontab, like "john", for example. You can do the following:
(sudo crontab -l -u john; echo "* * * * * command") | awk '!x[$0]++' | sudo crontab -u john -
Source for most of this solution: https://www.baeldung.com/linux/create-crontab-script
I was having tons of issues trying to add a job to another user's crontab. It kept duplicating crontabs, or just flat-out deleting them. After some testing, though, I'm confident this line of code will append a new job to a specified user's crontab, non-destructively, including not creating a job that already exists.
I wanted to find an example like this, so maybe it helps:
COMMAND="/var/lib/postgresql/backup.sh"
CRON="0 0 * * *"
USER="postgres"
CRON_FILE="postgres-backup"
# At CRON times, the USER will run the COMMAND
echo "$CRON $USER $COMMAND" | sudo tee /etc/cron.d/$CRON_FILE
echo "Cron job created. Remove /etc/cron.d/$CRON_FILE to stop it."

Resources