Crontab showing different time and Shell script when run manually shows different time stamp - linux

#!/bin/sh
string1=$(date +"%T")
string2=$(date -r merchant.xml +"%T")
StartDate=$(date -d "$string1" +"%s")
FinalDate=$(date -d "$string2" +"%s")
echo Since, $(date -d "0 $StartDate sec - $FinalDate sec" +"%H:%M") HOURS, mail has not been updated | mail -s "Merchant File Staleness" hello#gmail.com
it is my shell script name hp.sh and output is
Since, 00:55 HOURS, mail has not been updated.
The crontab is
0 * * * * /tmp/hp.sh
and output of crontab is
Since, 07:04 HOURS, mail has not been updated.
The output of both is different. I need output of my shell script using crontab every hour.

Check the TZ variable, both system and user-defined - cron will happily oblige to whatever is set there, even if it is different from system settings.

Related

How to check last running time of any script in linux instance

I want to check if my scripts ran the last night(or last ran timestamp) on linux instance based on scripts crontab running time stamp.
So how to get scripts last ran time on linux instance?
I would suggest better record the start time during the start of the script and end time at the end of the Script.
# Start Time Entry
echo "Start : " $(date +%T) > exec.log
start=`date +%s`
CALL YOUR SCRIPT HERE
# End Time Entry
end=`date +%s`
echo "End : " $(date +%T) >> exec.log
# Get the Runtime
runtime=$((end-start))
echo "Runtime: $runtime" >> exec.log
If there is any better way, I am also curious to see and implement too.
grep cron from your "messages" or "syslog
grep -i cron /var/log/messages
or create a separate log file for cron from rsyslog, edit file /etc/rsyslog.conf and change #cron to cron. You will find logs in /var/log/cron

Crontab not giving results

I am not in the root,
I entered the following commands in the crontab:
*/1 * * * * /home/ajain/testscript.sh
The file testscript.sh has the following commands:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date` >> /home/ajain/testscript.log
echo "The script ends now"
exit
The crontab is not giving the results, however, the following command is giving the result in the testscript.log file correctly, displaying the ping date.
bash testscript.sh
Why is the crontab not working?
You can fix it in two different ways.
To provide full path to the script /home/ajain/testscript.sh. Here you don't even need to add bash because you have clearly mentioned in which shell your script should run i.e. first line of your script #!/bin/bash
Add this line before executing the script
set path=$path:/home/ajain/
testscript.sh # no need to use bash in front of it
Also providing execution permission to a script is not just enough. You need to check whether the user who is going to execute the script has permission to the location of the script or not. That means whether user can do a cd /home/ajain/ or not.
Hope this will help you.
remove >> /home/ajain/script.log, just add line to the top of file /home/ajain/testscript.sh:
#!/bin/bash
exec >> /home/ajain/script.log 2>&1
echo "The script begins now"
ping -c 2 live.com
echo "The script has been run on `date`"
echo "The script ends now"
exit
remove >> /home/ajain/script.log from crontab, just using:
*/1 * * * * /home/ajain/testscript.sh
You need do define script output.
Because of cron man page:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in
the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen
in the syslog and ps output.
Fore everything else than echo The script has been run on 'date' >> /home/ajain/testscript.log you should check your/root's mail, or the syslog (eg. /var/log/syslog).
I could recommend make something like this:
*/1 * * * * /home/ajain/testscript.sh >> /home/ajain/script.log
#Edit
My crontab file on my user:
$crontab -l
# test
*/1 * * * * /home/3sky/tests/test.sh >> /home/3sky/tests/log.log
Script look like that:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date`
echo "The script ends now"
exit
Permission on files:
test.sh - 0755/-rwxr-xr-x
log.log - 0644/-rw-r--r--
Output in log file:
$tail -20 log.log
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:18:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:19:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:20:12 CEST 2018
The script ends now

Including Cron/Crontab in my Bash Backup Script with inputs

Im doing a Bash-Backup Script with 3 Options:
Do fullbackup
Do fullbackup at a specific time with cron
First I want to ask for the path like: Path of directory: /home
Then i want the hour for the backup: Hour for the backup (0:00-23:59) : 2:00
Then a simple question like: The backup will execute at 2:00. Do you agree(y/n)
I know how to do a crontab but I have no idea how to include that in my script so that I choose option 2 and then the script asks me for the directory and time and set then the crontab.
Any ideas or help would be appreciated!
You can get the existing crontab with
crontab -l
and install a new crontab with
crontab file
So your script would probably need something like
crontab -l | grep -v "# install-backup-script" > /tmp/file.$$
echo "$min $hour * * * /full/path/to/script # install-backup-script" >> /tmp/file.$$
crontab /tmp/file.$$
rm -f /tmp/file.$$

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Starting pppd from cron doesn't work

I want to start pppd whenever it disconnects. I am trying to setup a shell script to run every 1 minute to see if it's down and reconnect.
I have a bash script called vpn-check.sh:
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
When I run this script from cli directly, it works and starts ppp but when I run the same through cronjob (for root user), it doesn't work.
I tried the below and didn't work
*/1 * * * * bash /root/vpn-check.sh > /root/cronlog.txt 2>&1
I tried the below and didn't work
*/1 * * * * /root/vpn-check.sh > /root/cronlog.txt 2>&1
Finally, I tried:
*/1 * * * * /usr/sbin/pppd call VPNname> /root/cronlog.txt 2>&1
Can't figure out what could be wrong.
I still don't understand why some scripts work and don't work from cron when it is being called as the correct user according to the logs.
The only solution I found is to run:
crontab -e
and add the following lines to the top (even though I am calling the pppd daemon by the full path):
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I fixed it. All this while I was running crontab -e
but for the user name to be added, it needs to be added to the system-wide cron file found under /etc/crontab
user that starts the job can only be added in the above mentioned system wide cron file.
You are missing the shebang from your shell script.
vpn-check.sh should look like:
#!/bin/bash
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
See:
http://linuxconfig.org/bash-scripting-tutorial
What does the line "#!/bin/sh" mean in a UNIX shell script?
I was getting the same problem too.I used #dave's answer to figure it out. You just need to add the user name to the crontab, Add the next line to the end of /etc/crontab file:
*/1 * * * * root bash /root/vpn.sh
Replace the shell name of your own.

Resources