Script working fine from command line, but not from crontab - linux

My crontab entry as "ins" user is this:
* * * * * /usr/bin/ksh /apps/swbkp/swbkp.sh
The script is:
#! /usr/bin/ksh
. /apps/ins/.profile
cdate=$(date +'%y%m%d')
/apps/omni/bin/swmml -e "backup-node:" >> /apps/swbkp/swerrr1.$cdate
#
if [[ -f /apps/omni/conf/archive.C7M3UAA.500.$cdate ]]
then
mv -f /apps/omni/conf/archive.C7M3UAA.500.$cdate /apps/swbkp/
elif [[ -f /apps/omni/labeir1/dffile/archive.C7M3UAA.500.$cdate ]]
then
mv -f /apps/omni/labeir1/dffile/archive.C7M3UAA.500.$cdate /apps/swbkp/
else
printf "Backup archive File not present to move"
fi >> /apps/swbkp/swerrr1.$cdate
#
Note: /apps/omni/bin/swmml -e "backup-node:" this line simply creates a backup file on my system of type archive.C7M3UA.500.<current date>
2 weird things happening:
the backup file generated is:
-rw-r--r-- 1 root root 165 Aug 28 21:55 /apps/omni/labeir1/dffile/archive.C7M3UAA.500.130828
When getting moved to /apps/swbkp, the timestamp is 1 min before:
-rw-r--r-- 1 root root 165 Aug 28 21:54 archive.C7M3UAA.500.130828
Nothing is getting redirected to /apps/swbkp/swerrr1.$cdate file
-rw-r--r-- 1 ins ins 0 Aug 28 21:24 swerrr1.130828
Whereas when I run the script from terminal, everything works perfect, i.e the file has the same timestamp in backup folder as well as moved folder, and outputs are getting logged in the log file as well.
Kindly help

This may already have been answered, but for anyone looking for help:
You'll need to escape the 'percent' like below when using date/time stamps in crons:
cdate=$(date +'\%y\%m\%d') instead of cdate=$(date +'%y%m%d')
if using this in yaml/ Ansible playbooks, you'll need to double escape or escape the escape like:
$(date +'\\%y\\%m\\%d')
Eventually the cron should look something like
* * * * * script.sh > /tmp/script_$(date +\%y\%m\%d).log 2>&1

Take a look at /apps/ins/.profile and the files it executes - such files usually have a conditional that exits the script early if it's not run in a terminal.
For example:
[ -z "$PS1" ] && return
This may change the behavior of your script (or even skip it, if exit is used in place of return). At the very least, you will miss aliases, possible PATH changes, and other things set up in the .profile script, which will affect if and how your main script is run.
Try to comment the line . /apps/ins/.profile in the script above, and see if it still runs in the terminal. If it does, run it like that from crontab and see if it fixes your problem.

Related

Cron not executing the shell script + Linux [duplicate]

I have a script that checks if the PPTP VPN is running, and if not it reconnects the PPTP VPN. When I run the script manually it executes fine, but when I make a cron job, it's not running.
* * * * * /bin/bash /var/scripts/vpn-check.sh
Here is the script:
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
finally i found a solution ... instead of entering the cronjob with
crontab -e
i needed to edit the crontab file directly
nano /etc/crontab
adding e.g. something like
*/5 * * * * root /bin/bash /var/scripts/vpn-check.sh
and its fine now!
Thank you all for your help ... hope my solution will help other people as well.
After a long time getting errors, I just did this:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
Where test.sh contains:
#!/bin/bash
/usr/bin/python3 /home/joaovitordeon/Documentos/test.py;
In my case, the issue was that the script wasn't marked as executable. To make sure it is, run the following command:
chmod +x your_script.sh
If you're positive the script runs outside of cron, execute
printf "SHELL=$SHELL\nPATH=$PATH\n* * * * * /bin/bash /var/scripts/vpn-check.sh\n"
Do crontab -e for whichever crontab you're using and replace it with output of the above command. This should mirror most of your environment in case there is some missing path issue or something else. Also check logs for any errors it's getting.
Though it definitly looks like the script has an error or you messed something up when copying it here
sed: -e expression #1, char 44: unterminated `s' command
./bad.sh: 5: ./bad.sh: [[: not found
Simple alternate script
#!/bin/bash
if [[ $(ping -c3 192.168.17.27) == *"0 received"* ]]; then
/usr/sbin/pppd call home
fi
Your script can be corrected and simplified like this:
#!/bin/sh
log=/tmp/vpn-check.log
{ date; ping -c3 192.168.17.27; } > $log
if grep -q '0 received' $log; then
/usr/sbin/pppd call home >>$log 2>&1
fi
Through our discussion in comments we confirmed that the script itself works, but pppd doesn't, when running from cron. This is because something must be different in an interactive shell like your terminal window, and in cron. This kind of problem is very common by the way.
The first thing to do is try to remember what configuration is necessary for pppd. I don't use it so I don't know. Maybe you need to set some environment variables? In which case most probably you set something in a startup file, like .bashrc, which is usually not used in a non-interactive shell, and would explain why pppd doesn't work.
The second thing is to check the logs of pppd. If you cannot find the logs easily, look into its man page, and it's configuration files, and try to find the logs, or how to make it log. Based on the logs, you should be able to find what is missing when running in cron, and resolve your problem.
Was having a similar problem that was resolved when a sh was put before the command in crontab
This did not work :
#reboot ~myhome/mycommand >/tmp/logfile 2>&1
This did :
#reboot sh ~myhome/mycommand >/tmp/logfile 2>&1
my case:
crontab -e
then adding the line:
* * * * * ( cd /directory/of/script/ && /bin/sh /directory/of/script/scriptItself.sh )
in fact, if I added "root" as per the user, it thought "root" was a command, and it didn't work.
As a complement of other's answers, didn't you forget the username in your crontab script ?
Try this :
* * * * * root /bin/bash /var/scripts/vpn-check.sh
EDIT
Here is a patch of your code
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult=`echo "$result" | /bin/sed 's/^\(.................................\).*$/\1/'`
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
In my case, it could be solved by using this:
* * * * * root ( cd /directory/of/script/ && /directory/of/script/scriptItself.sh )
I used some ./folder/-references in the script, which didn't work.
The problem statement is script is getting executed when run manually in the shell but when run through cron, it gives "java: command not found" error -
Please try below 2 options and it should fix the issue -
Ensure the script is executable .If it's not, execute below -
chmod a+x your_script_name.sh
The cron job doesn’t run with the same user with which you are executing the script manually - so it doesn't have access to the same $PATH variable as your user which means it can't locate the Java executable to execute the commands in the script. We should first fetch the value of PATH variable as below and then set it(export) in the script -
echo $PATH can be used to fetch the value of PATH variable.
and your script can be modified as below - Please see second line starting with export
#!/bin/sh
export PATH=<provide the value of echo $PATH>
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
First of all, check if cron service is running. You know the first question of the IT helpdesk: "Is the PC plugged in?".
For me, this was happening because the cronjob was executing from /root directory but my shell script (a script to pull the latest code from GitHub and run the tests) were in a different directory. So, I had to edit my script to have a cd to my scripts folder. My debug steps were
Verified that my script run independent of cron job
Checked /var/log/cron to see if the cron jobs are running. Verified that the job is running at the intended time
Added an echo command to the script to log the start and end times to a file. Verified that those were getting logged but not the actual commands
Logged the result of pwd to the same file and saw that the commands were getting executed from /root
Tried adding a cd to my script directory as the first line in the script. When the cron job kicked off this time, the script got executed just like in step 1.
it was timezone in my case. I scheduled cron with my local time but server has different timezone and it does not run at all. so make sure your server has same time by date cmd
first run command env > env.tmp
then run cat env.tmp
copy PATH=.................. Full line and paste into crontab -e, line before your cronjobs.
try this
/home/your site folder name/public_html/gistfile1.sh
set cron path like above

inconsistent results from shell script mv

I'm having an issue with a simple shell script that merely moves a file from one location to another. It takes the source & target paths & names as parameters. Code is as follows...
#!/bin/sh
if [ ! -f $1 ]; then
exit 1
else
ls -l $1
mv -v $1 $2
X=$?
echo 'Move completion code = ' $X
ls -l $2
exit $X
fi
This script is called multiple times in a sequential job schedule, loading data to (replacing) the same file. Recently seeing instances where the job after the move script runs with data from the file that existed before - indicating that it was not overwritten - yet the messages in the move script show a successful move - and checking the file itself shows it with the expected data. Any ideas? Could the script be completing before the mv command has fully completed? I've put the completion code message in to check for this, I've also tried sleeping for 5 sec. What am I missing?
Thanks!
Update...
Here's the output after adding the md5sum checks....
-rw-r--r--. 1 appworx dba 22 Aug 10 12:15 UP.dat
Before move - file 1
9bf394b473cf389d704357e4da46fcfa UP.dat
`UP.dat' -> `ids.dat'
removed `UP.dat'
After move - file 2
9bf394b473cf389d704357e4da46fcfa
Move completion code = 0
-rw-r--r--. 1 appworx dba 22 Aug 10 12:15 load_ids.dat

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.

Bash Script works but not in when executed from crontab

I am new to linux and the script below is just an example of my issue:
I have a script which works as expected when I execute it however when I set it to run via crontab it doesn't work as expected because it doesn't read the file content into the variable.
I have a file 'test.txt' which has 'abc' in it. My script puts the text into a variable 'var' and then I echo it out to a log file:
var=$(</home/pi/MyScripts/test.txt)
echo "$var" >/home/pi/MyScripts/log.log
This works perfectly fine when I execute it and it echo's into the log file but not when I set it via crontab:
* * * * * /home/pi/MyScripts/test.sh
The cron job runs, and it sent me the following error message:
/bin/sh: 1: /home/pi/MyScripts/test.sh: Permission denied.
But I have given it 777 permissions:
-rwxrwxrwx 1 pi pi 25 Jun 10 15:31 test.txt
-rwxrwxrwx 1 pi pi 77 Jun 10 15:34 test.sh
Any ideas?
This happens when you run the script with a different shell. It's especially relevant for systems where /bin/sh is dash:
$ cat myscript
echo "$(< file)"
$ bash myscript
hello world
$ sh myscript
$
To fix it, add #!/bin/bash as the first line in your script.
Others have provided answers, but I will give you a big clue from your error message; emphasis mine:
/bin/sh: 1: /home/pi/MyScripts/test.sh: Permission denied.
Note how the cron job was trying to use /bin/sh to run the script. That’s solved by always indicating which shell you want to use at the top of your script like this.
#!/bin/bash
var=$(</home/pi/MyScripts/test.txt)
echo "$var" >/home/pi/MyScripts/log.log
If your script is using bash, then you must explicitly set /bin/bash in some way.
Also, regarding permissions you say this:
But I have given it 777 permissions:
First, 777 permissions is a massive security risk. If you do that it means that anyone or anything on the system can read, write & execute the file. Don’t do that. In the case of a cron job the only entity that needs 7 permissions on a file is the owner of the crontab running that file.
Meaning if this is your crontab, just change the permissions to 755 which allows others to read & execute but not write. Or maybe better yet change it to 700 so only you—as the owner of the file—can do anything to the file. But avoid 777 permissions if you want to keep your system safe, stable & sane.
You have two options. In the first line of your file, tell what program you want to interpret the script
#!/bin/bash
...more code...
Or in your crontab, tell what program you want to interpret the script
* * * * * bash /home/pi/MyScripts/test.sh
In this option, you do not need to make the script executable

Shell script to log server checks runs manually, but not from cron

I'm using a basic shell script to log the results of top, netstat, ps and free every minute.
This is the script:
/scripts/logtop:
TERM=vt100
export TERM
time=$(date)
min=${time:14:2}
top -b -n 1 > /var/log/systemCheckLogs/$min
netstat -an >> /var/log/systemCheckLogs/$min
ps aux >> /var/log/systemCheckLogs/$min
free >> /var/log/systemCheckLogs/$min
echo "Message Content: $min" | mail -s "Ran System Check script" email#domain.com
exit 0
When I run this script directly it works fine. It creates the files and puts them in /var/log/systemCheckLogs/ and then sends me an email.
I can't, however, get it to work when trying to get cron to do it every minute.
I tried putting it in /var/spool/cron/root like so:
* * * * * /scripts/logtop > /dev/null 2>&1
and it never executes
I also tried putting it in /var/spool/cron/myservername and also like so:
* * * * * /scripts/logtop > /dev/null 2>&1
it'll run every minute, but nothing gets created in systemCheckLogs.
Is there a reason it works when I run it but not when cron runs it?
Also, here's what the permissions look like:
-rwxrwxrwx 1 root root 326 Jul 21 01:53 logtop
drwxr-xr-x 2 root root 4096 Jul 21 01:51 systemCheckLogs
Normally crontabs are kept in "/var/spool/cron/crontabs/". Also, normally, you update it with the crontab command as this HUPs crond after you're done and it'll make sure the file gets in the correct place.
Are you using the crontab command to create the cron entry? crontab to import a file directly. crontab -e to edit the current crontab with $EDITOR.
All jobs run by cron need the interpreter listed at the top, so cron knows how to run them.
I can't tell if you just omitted that line or if it is not in your script.
For example,
#!/bin/bash
echo "Test cron jon"
When running from /var/spool/cron/root, it may be failing because cron is not configured to run for root. On linux, root cron jobs are typically run from /etc/crontab rather than from /var/spool/cron.
When running from /var/spool/cron/myservername, you probably have a permissions problem. Don't redirect the error to /dev/null -- capture them and examine.
Something else to be aware of, cron doesn't initialize the full run environment, which can sometimes mean you can run it just fine from a fully logged-in shell, but it doesn't behave the same from cron.
In the case of above, you don't have a "#!/bin/shell" up top in your script. If root is configured to use something like a regular bourne shell or cshell, the syntax you use to populate your variables will not work. This would explain why it would run, but not populate your files. So if you need it to be ksh, "#!/bin/ksh". It's generally best not to trust the environment to keep these things sane. If you need your profile run the do a ". ~/.profile" up front as well. Or a quick and dirty way to get your relatively full env is to do it from su as such "* * * * * su - root -c "/path/to/script" > /dev/null 2>&1
Just some things I've picked up over the years. You're definitely expecting a ksh based on your syntax, so you might want to be sure it's using it.
Thanks for the tips... used a little bit of each answer to get to the bottom of this.
I did have the interpreter at the top (wasn't shown here), but may have been wrong.
Am using #!/bin/bash now and that works.
Also had to tinker with the permissions of the directory the log files are being dumped in to get things working.

Resources