Cronjob is not executing a shellscript - shell

The cronjob executes this script without a problem and kills the screen:
This is the cronjob for it:
47 14 * * * sh /home/server/sidelife/fivem/stop.sh >/dev/null 2>&1
screen -X -S FiveM quit
My other script is entered into cron like this:
53 14 * * * /usr/bin/sh /home/server/sidelife/fivem/start.sh >/dev/null 2>&1
The script looks like this:
#!/usr/bin/sh
cd /home/server/sidelife/fivem
screen -S FiveM /home/server/sidelife/download/run.sh +exec /home/server/sidelife/fivem/server.cfg
This isn't starting a screen neither is it doing anything, I do not know where the problem is since the first one works.

Related

Run a bash script via cron and save variable to be used in the cronjob

I have the following cronjob running on RHEL 7.5:
5 12 * * * root /mydir/myscript.sh 2>&1 | mailx -s "My Script: myscript.sh has run" root#mycompany.com
The script myscript.sh basically will output a result at the end of it - 0 for Success and 1 for failures. This is stored in the variable $result.
My question: is it possible to have $result be read in the cronjob so I can change it to something like:
5 12 * * * root /mydir/myscript.sh 2>&1 | mailx -s "My Script: myscript.sh has run with error code $result" root#mycompany.com
This way I can tell from the subject whether the script has run successfully or not.
So far I havent found a way to save $result into a variable that keeps its value that can be read by cron. Is this possible (I'm sure some of your geniuses out there will have a solution!)
UPDATE:
I know that I can send an email from the script itself but there is a requirement that prevents me from doing this so it has to be done from the cronjob itself.
Thanks J
One way to do it is like this
5 12 * * * /bin/bash -l -c 'result=`/mydir/myscript.sh` | mailx -s "My Script: myscript.sh has run with error code $result" root#mycompany.com'
Another way is just to update myscript to send the email

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

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

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.

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

Shell Script works fine manually, but fails to exectue through Crontab

So i can't seem to fix this issue i'm having with Crontab. I have a shell script i want to run every 15 minutes. I can execute the script manually but Crontab will not launch it successfully. It's weird, I'll set it to execute at a certain time and i can check that the process is running, but it never actually executes. Here's the settings i have in Crontab.
0, 15, 30, 45 * * * root /home/rpitc/Desktop/Script/Refresh
Here's what the Shell Script looks Like.
#!/bin/bash
service=wfica
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
/home/rpitc/Desktop/Script/Iceweasel.sh & /home/rpitc/Desktop/Script/Login.sh
fi
I've read on here that it could be the path that's creating the issue so what i did was copy all of scripts to the /bin path changed the scripts appropriately, but it still would not execute. Please help, this is being ridiculous!
I think the syntax should be (whitout "root" and without spaces), when using "crontab -e":
0,15,30,45 * * * /home/rpitc/Desktop/Script/Refresh
If you're using a file in "/etc/cron.d/", the crontab entry should look like:
0,15,30,45 * * * root /home/rpitc/Desktop/Script/Refresh

Resources