Shell script runs manually but not executed through cron job - linux

I am using a shell script to delete log files in my Linux machine. And I have used crontab to execute this job periodically. I can execute this script manually and see it is working fine. But the script is not triggered through cron job.
Shell script:
[root#testVM-dev log]# vim /path_to_dir/log_cleanup.sh
#!/bin/bash
now=$(date)
echo "Cron Job to delete log files start...# $now" | tee -a /path_to_dir/log/cronjob_log.log
/usr/bin/find /path_to_dir/log/localhost_access_log.* -type f -mtime +5 -print -delete | tee -a /path_to_dir/log/cronjob_log.log
echo "Delete files completed!" | tee -a /path_to_dir/log/cronjob_log.log
crontab entry: (Runs every minute)
[root#testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
Also I have checked in cron logs that the job is executed every minute. But I couldn't see any logs in the targeted log file "cronjob_log.log"
cron logs:
vim /var/log/cron
Jul 16 03:56:01 rstestVM-dev crond[19131]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:57:01 rstestVM-dev crond[19150]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:58:01 rstestVM-dev crond[19168]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:59:01 rstestVM-dev crond[19188]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Can someone help me on what is going wrong here?

Besides manually specifying value of PATH, try specifying the path to tee and date.
/usr/bin/date, /usr/bin/tee

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?

Related

Running aide --check as a crontab job once a week

I'm having real problems getting a simple (aide --check) job to run as a crontab job. I've tried everything I can think of but it won't seem to run. I've tried specifying the shell in crontab:
SHELL=/bin/bash
I've tried all kinds of variations of the command line:
*/1 * * * * root /bin/bash /usr/sbin/aide --check
*/1 * * * * /bin/bash /usr/sbin/aide --check
*/1 * * * * root /usr/sbin/aide --check
*/1 * * * * root /bin/bash /usr/sbin/aide --check >> /var/log/SystemFileCheck.log
Plus others but just can't get it to run. I've followed online guides which all say I'm doing it correctly. I've tried putting it into a bash script instead and running that but no luck. What am I doing wrong?
These are some of the errors I'm getting:
Mar 30 11:25:01 localhost CROND[14060]: (root) CMD (root /bin/bash /usr/sbin/aide --check >> /var/log/SystemFileCheck.log)
Mar 30 11:25:01 localhost CROND[14058]: (root) CMDOUT (/bin/sh: root: command not found)
Mar 30 11:28:01 localhost CROND[14397]: (root) CMD (root /bin/SystemIntegCheck.sh >> /var/log/SystemFileCheck.log)
Mar 30 11:28:01 localhost CROND[14395]: (root) CMDOUT (/bin/bash: root: command not found)
Mar 30 11:39:01 localhost CROND[16094]: (root) CMD (/bin/bash /usr/sbin/aide --check)
Mar 30 11:39:01 localhost CROND[16092]: (root) CMDOUT (/usr/sbin/aide: /usr/sbin/aide: cannot execute binary file)
Can anyone shed some light on this?
Thanks in advance
PS. the once a minute is just for testing
A user id can only be specified in the system crontab file. The entries of a user's crontab file don't take a user id. The entries in question are apparently found in a user's crontab file, which is why you get root: command not found from the first, third and fourth entries.
From the second, you get cannot execute binary file because you ask bash to execute /usr/sbin/aide as a bash script when it's not a bash script. You should be using
*/1 * * * * /usr/sbin/aide --check

crontab not working despite trying different fixes from Stackoverflow

I'm trying to run some cron jobs as root but it seems that my crontab it's not working. After reading a lot of threads here in SO I've changed these things:
I've removed the sudos inside the crontab.
I've added the full path of all the commands inside the crontab.
I've restarted the cron job each time I've changed it.
But it's not working yet.
This is my current crontab (I call it from my sudo user with "sudo crontab -e" to run it as root):
0 4 * * * /usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
5 4 * * * /usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
30 4 * * * /opt/letsencrypt/certbot-auto renew > /var/log/log3.log
35 4 * * * /etc/init.d/nginx reload > /var/log/log4.log
40 4 * * * /bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
What I'm trying to do with these 5 commands is:
At 04:00. Remove all backups that are more than 7 days old.
At 04:04. Backup all my mongodb databases.
At 04:30. Try to renew my SSL certificate.
At 04:35. Reload Nginx service.
At 04:40. Update my mongodb.pem file with the new certificate.
It's important to say that each of these commands running from the shell one by one with sudo are working properly.
But from the cron the results are:
I don't know if this command is working. The log file (log1.log) is empty.
It's not working. No backup have been created. The log file (log2.log) has not been created.
It seems that it's working. The log file (log3.log) shows the standard output when you run this command correctly.
It seems that it's working. The log file (log4.log) shows the standard output when you run this command correctly.
It seems that it's not working. The mongo.pem file has the correct updated date but the file is empty. The files fullchain1.pem and privkey1.pem have the correct content, so it seems a problem with "/usr/bin/tee /etc/ssl/mongo.pem". The log file (log5.log) has not been created.
Finally the cron.log shows this:
Nov 11 04:00:01 myservername CRON[31286]: (root) CMD (/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log)
Nov 11 04:05:01 myservername CRON[31297]: (root) CMD (/usr/bin/mongodump --out /var/backups/mongobackups/`date +")
Nov 11 04:05:01 myservername CRON[31296]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:07:01 myservername CRON[31306]: (root) CMD ( test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Nov 11 04:17:01 myservername CRON[31325]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 11 04:30:01 myservername CRON[31353]: (root) CMD (/opt/letsencrypt/certbot-auto renew > /var/log/log3.log)
Nov 11 04:30:04 myservername CRON[31352]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:35:01 myservername CRON[31393]: (root) CMD (/etc/init.d/nginx reload > /var/log/log4.log)
Nov 11 04:40:01 myservername CRON[31420]: (root) CMD (/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log)
Nov 11 04:40:01 myservername CRON[31419]: (CRON) info (No MTA installed, discarding output)
If I'm not wrong the error "No MTA installed, discarding output" is not a problem because it's related to sending the output by email, am I right?
So anyone could tell me what is wrong with my crontab?
Thank you very much!
I don't know what is the problem with my crontab but this is working if I put all these commands inside a script and call this script inside the crontab.
The script (myscript.sh):
#!/bin/bash
/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
/usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
/opt/letsencrypt/certbot-auto renew > /var/log/log3.log
/etc/init.d/nginx reload > /var/log/log4.log
/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
The crontab:
00 03 * * * <pathtomyscript>/myscript.sh
This is working now.

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.

Unable to find a cron file

I have to migrate a server to a new cloud provider. In doing so I need to copy all the LAMP code to new virtual machine. I did all that. However, on the old server one cron job runs which does something. I'm unable to find the cron file and migrate it to new server. The output of:
grep CRON /var/log/syslog
is
Nov 5 19:00:01 frroleapi CRON[22975]: (root) CMD (php /var/www/api/scheduler.php)
Nov 5 19:05:01 frroleapi CRON[23189]: (root) CMD (php /var/www/api/scheduler3.php)
Nov 5 19:05:01 frroleapi CRON[23190]: (root) CMD (php /var/www/api/scheduler.php)
Nov 5 19:09:01 frroleapi CRON[23382]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -delete)
The last cronjob is a file php5 in /etc/cron.d. However, I'm unable to find the cron files for earlier entries.
You should be able to open the list of scheduled cron events with the crontab command. This should also show you what files run during the cron and where they are located:
crontab -l
You can then add these cron entries to the new server using crontab with the edit parameter, like this:
crontab -e
For more information, see also: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
This tutorial may also be helpful for understanding cron and crontab: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/

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