Cronjob failing on Debian - cron

I'm having no idea why my cronjob isn't executed. Here are the details:
I've a script inside /usr/src named stundenplan.sh, which executes a jar file.
The script itself works if you are running it from the console.
But trying to run it from a cronjob it doesn't work.
The script looks like this:
#!/bin/sh
java -jar vorlesungsplaene.jar
The jar file lies in the same directory as expected.
I defined two cronjobs:
# m h dom mon dow command
30 * * * * /usr/src/stundenplan.sh
0-59 * * * * echo "dude" >> /tmp/test.txt
The job below works fine, writing "dude" meticulously in the test.txt file.
As you can see in the syslog, the job above fails:
Jul 18 15:29:01 vps47327 /USR/SBIN/CRON[16361]: (root) CMD (echo "dude" >> /tmp/test.txt)
Jul 18 15:30:01 vps47327 /USR/SBIN/CRON[16364]: (root) CMD (/usr/src/stundenplan.sh)
Jul 18 15:30:01 vps47327 /USR/SBIN/CRON[16365]: (root) CMD (echo "dude" >> /tmp/test.txt)
Jul 18 15:30:01 vps47327 /USR/SBIN/CRON[16363]: (CRON) error (grandchild #16364 failed with exit status 1)
Any suggestions, ideas or solutions? ;)

It looks like a path issue. Crontab is probably being run from some arbitrary directory so it can't find the jar file.
Try doing:
30 * * * * cd /usr/src && ./stundenplan.sh
That's not really the prettiest way to go about it, but if it works, then at least you know what the problem is. Possibly it might be more elegant to put the full path of the jar file in the script then, but I'll leave that question to you. :)
Hope it helps!

Related

Cron.d file not running

I am new to Linux and have a problem that I have tried to look online and could not find a solution
I have 4 scripts that are running in cron.d 3 of them I set to run every minute and they are fine and logging into the output files but the last one should run at 1:00 am but it will not.
-rw-r--r-- 1 root root 390 Jul 29 14:03 Test
* * * * * root /usr/sa/dir1/dir2/script1.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script2.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script3.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
0 1 * * * root /usr/sa/dir1/dir2/script4.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
I checked the permission and all seems to be fine as the same script from cron.d file are running as I can see entries from cron that are executed in /var/log/messages and same from the log files.
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
Thank you in advance for the help
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
I don't see errors in /var/log/messages
Are you commiting this crontab by just editing the raw file or are you using crontab <my-new-crontab>? If you aren't then try the latter. :)
I end it up adding the scripts to the crontab of the user root and seems to be ok.

starting a python3 script in ubuntu at boot and wont execute properly using cron /etc/init.d rc.local systemd

I've tried 3 methods on starting python scripts at startup:
Adding the script line with full paths to python3 bin and the script to rc.local (verifying permissions are executable)
This option will execute shell scripts and other commands but for some reason it just doesn't execute my python script. Well actually, it looks like it might but it never finishes cause I dont get the output file showing it worked.
adding to /etc/init.d and creating a symlink
Adding a #reboot in cron
Creating a service with systemd that executes it
All of them appear like they might be triggering the script but like on the last one, I have the script being launched by cron, and then the script removes the cron job that started the script. When I execute the script from the command line normally, it works perfectly. However, when cron starts it, it appears to partially execute cause the /etc/crontab file gets deleted.
Does this have something to do with the context of the file being deleted?
I tried tagging 2> to pretty much all other methods above to see if errors occur but I literally get nothing in the stderr file. Also added the stderr/print to rc.local.
Here is my script
#!/usr/bin/env python3
import time
import subprocess
import re
cron = open("/etc/crontab","r")
print("1")
cronText = cron.read()
cron.close
print("2")
cron = open("/etc/crontab","w")
print("3")
cron.write('')
print("4")
cronText = re.sub(r'\* \* \* \* \* root /usr/bin/env python3 /root/Desktop/test\.py 2\> /root/Desktop/err\.log','',cronText)
print("5")
cron.write(cronText)
cron.close
print("6")
subprocess.call("service cron restart",shell=True)
print("7")
file1 = open("thisWORKED.txt","w")
print("8")
file1.write("This totally worked")
file1.close
print("9")
Here is what /etc/crontab looks like
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
* * * * * root /usr/bin/env python3 /root/Desktop/test.py > /root/Desktop/err.log 2>&1
The err.log literally gives nothing in it output. I tried tagging stderr like everywhere when I tried to other methods. Im mostly just trying to figure out why my scripts arent being started.
Ok lol I figured out what I was doing wrong. I feel like a dummy now. Basically, it runs under the context of the root user so the successful log file is being dumped out to /root instead of /home where the script is being run from. Hence, why there were no errors to report or see in the stderr.

Cron run every minute ( Runs in bash but not in cron)

This have been discussed several times in previous post. I followed given advise but it does not work for me. I have two scripts which are run by the cron service every minute. To my surprise, only one runs per minute( 1st in the list below), the other fails (2nd in list below). Most surprising, when run direct from the terminal, both scripts execute fine.
Cron setup :
*/1 * * * * /home/user/Desktop/scripts/generatepattern.sh
*/1 * * * * /home/user/Desktop/scripts/getnextfile.sh
File permissions are:
-rwxr--r-- 1 user user 522 Jul 25 16:18 generatepattern.sh
-rwxr--r-- 1 user user 312 Jul 25 23:02 getnextfile.sh
The code for the non-schedulable( not running in cron ) is :
#!/bin/bash
#Generate a file to be used for the search
cd /home/user/Desktop/scripts
no=`cat filecount.txt`
if test $no -lt 20
then
#echo "echo less"
#echo $no
expr `cat filecount.txt` + 1 >filecount.txt
fi
In the last line you wrote cat filecount.txt instead of cat /home/user/Desktop/scripts/filecount.txt
I discovered the main issue was that the new cron settings only get used when the vi editor gets closed. Changes have to be made on the editor and a :wq command issued so that the new settings get installed. Just issuing :w command does not work since no install happens(this was my mistake). I realised this after issuing :wq command on vi and the following output displayed :-
# crontab -e
crontab: installing new crontab
Thanks to all other suggestions made.

cronjob will not start on weekday

I have to start a cronjob on every Thursday. Here is the script.
It will not start at all.
Manually it does his work but not as a cronjob.
It should start at 17.00 every Thursday:
00 17 * * 4 root cd /var/www/domein.nl/admin/scripts && php -f send_newsletter_subscribers.php
also tried to do as text: wed
33 15 * * wed root cd /var/www/domein.nl/admin/scripts && php -f send_newsletter_subscribers.php
Have no idea why it doesn't work.
Does anybody have any suggestion what I'm doing wrong?
Thanks in advance for replying.
Is this an individual user's crontab (edited with crontab -e) or a system level crontab file? If the former, then the syntax is wrong, and you need to remove the user specification ("root").
The time and date fields look fine.
Think about setting some important variables in your /etc/initab (specially PATH and SHELL).
My /etc/initab file contains the following:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/root

Something wrong with my crontab?

I am going to create a crontab task to schedule my task.
My /etc/crontab looks like this,
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
17 15 * * * root sh /opt/app/tool/ReviewSummaryTool/runf.sh
The task script runf.sh has content looks like this,
#!/usr/bin/env bash
java -Dhostname=$(hostname) -jar ReviewSummaryTool.jar -full
But the crontab task could not be executed (I checked the output log) when time is arrived.
However, the task script could be execute by command below,
sh /opt/app/tool/ReviewSummaryTool/runf.sh
And I checked the log of crontab at /var/log/cron and it seems that the task has already been executed. See brief log content below,
Aug 31 15:17:01 SSECBIGDATA01 crond[1677]: (*system*) RELOAD (/etc/crontab)
Aug 31 15:17:01 SSECBIGDATA01 CROND[29248]: (root) CMD (sh /opt/app/tool/ReviewSummaryTool/runf.sh)
Now, I have no idea what's wrong with my configuration. My operating system is CentOS.
Any help would be appreciate. Thanks in advance.
For debugging purposes replace sh with /bin/sh -vx in the crontab entry:
17 15 * * * root /bin/sh -vx /opt/app/tool/ReviewSummaryTool/runf.sh
Then the trace of the executed script will be printed, so emailed to you.
And add logger commands inside your runf.sh script, e.g. have it be
#!/bin/sh
logger -t runfjob start of runf pid $$ in $(pwd) on host $(hostname)
java -Dhostname=$(hostname) -jar ReviewSummaryTool.jar -full
logger -t runfjob end of runf pid $$
The logger command makes entries to the system log using syslog(3). You should find these messages under /var/log, perhaps in /var/log/messages or /var/log/syslog etc...
I strongly suggest to put the full path of the ReviewSummaryTool.jar file in the java command of your runf.sh script. It is likely that your cronjob is run in a current directory not having that file. Or perhaps put a cd command before the java one.
Be sure that the $PATH is correct and that java is found there.

Resources