Shell script not working in cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script not working in cron
# 1  
Old 01-20-2010
Question Shell script not working in cron

Hello,
I know little about shell scripting and creating a script, and worked fine in the command line. But not work in the cron. Below you could see the script

Code:
#!/bin/sh
LOGFILE=/home/transfield/mou/test.log
# Find yesterday Date and copy files
TODAY=$(date --date= +%F)
YESTERDAY=$(date --date="1 day ago" +%F)
YESTERDAY1=$(date --date="2 day ago" +%F)
echo $TODAY >> $LOGFILE
echo $YESTERDAY >> $LOGFILE
echo "## Copying processed files ##" >> $LOGFILE
ls -l /var/lct/mou2/processed | grep $TODAY | awk '{print " " $8}' > /home/trans/mou/processedfiles
ls -l /var/lct/mou2/processed | grep $YESTERDAY | awk '{print " " $8}' >> /home/trans/mou/processedfiles
exit


So far I found when I use corn following part not working, nothing goes to the processedfiles file.
Code:
ls -l /var/lct/mou2/processed | grep $TODAY | awk '{print " " $8}' > /home/trans/mou/processedfiles
ls -l /var/lct/mou2/processed | grep $YESTERDAY | awk '{print " " $8}' >> /home/trans/mou/processedfiles

This work perfect in command line. Corn job and command line use by the same user.
Hope some one could direct me to correct path.

thank you,

Last edited by pludi; 01-21-2010 at 03:47 AM.. Reason: code tags, please...
# 2  
Old 01-21-2010
This is a topic which very often comes up. Starting something on the command line is not the same environment like when started via cron. Environment variables will not be set like PATH and so on.
You can either use the search function of the forum and/or read this:
https://www.unix.com/answers-frequent...n-crontab.html
# 3  
Old 01-21-2010
Please post the exact cron line and state when you expect the cron to run.
Please state which Operating System you have.

Remember that the script will need to be invoked with the full path name of the script (not as ./scriptname).

If the cron is failing for some reason the error messages will be in mail for the cron user (e.g. root). Also check that the cron runs at all by looking at the logfile for your cron (see "man cron" to find the name of the logfile). How did you add the job to the crontab?
# 4  
Old 01-21-2010
MySQL Shell script not working in cron

I'm using Ubuntu 9.10 server 64 bit.
I changed the location of the script to simplify the test
so far I try following cron lines and syslog show script executed but nothing goes to the processedfiles
49 08 * * * /home/malik/test/Tmoustart
02 09 * * * sh /home/malik/test/Tmoustar

This is my modified script.
Code:
#!/bin/bash
. $HOME/.profile
LOGFILE=/home/malik/test/test.log
# Find yesterday Date and copy files
TODAY=$(date --date= +%F)
YESTERDAY=$(date --date="1 day ago" +%F)
YESTERDAY1=$(date --date="2 day ago" +%F)

echo $TODAY >> $LOGFILE
echo $YESTERDAY >> $LOGFILE
echo $YESTERDAY1 >> $LOGFILE
echo "## Copying prossed files ##" >> $LOGFILE
ls -l /var/lct/mou2/processed | grep $TODAY | awk '{print " " $8}' > /home/malik/test/processedfiles
ls -l /var/lct/mou2/processed | grep $YESTERDAY | awk '{print " " $8}' >> /home/malik/test/processedfiles
exit

Still I can see when I run on the terminal worked fine. But in the cron following part with ls not working.
Code:
ls -l /var/lct/mou2/processed | grep $TODAY | awk '{print " " $8}' > /home/malik/test/processedfiles
ls -l /var/lct/mou2/processed | grep $YESTERDAY | awk '{print " " $8}' >> /home/malik/test/processedfiles

I add . $HOME/.profile to pick-up the environment variables. Hope that's the correct way.

---------- Post updated at 11:18 AM ---------- Previous update was at 09:05 AM ----------

Now I narrow it down to grep command

I use following simple script
Code:
#!/bin/bash
ls -l /home/malik/test
echo "1 done ###"
ls -l /home/malik/test | grep 2010-01-22

Output from command line
Code:
malik@sysdb:~/test$ ./greptest
total 12
-rwxr-xr-x 1 malik malik   97 2010-01-22 11:04 greptest
-rw-r--r-- 1 malik malik    0 2010-01-22 10:28 processedfiles
-rw-r--r-- 1 malik malik 2501 2010-01-22 10:55 test.log
-rwxr-xr-x 1 malik malik  715 2010-01-22 10:54 Tmoustart
1 done ###
-rwxr-xr-x 1 malik malik   97 2010-01-22 11:04 greptest
-rw-r--r-- 1 malik malik    0 2010-01-22 10:28 processedfiles
-rw-r--r-- 1 malik malik 2501 2010-01-22 10:55 test.log
-rwxr-xr-x 1 malik malik  715 2010-01-22 10:54 Tmoustar


Output from cron
Code:
total 12
-rwxr-xr-x 1 malik malik  715 Jan 22 10:54 Tmoustart
-rwxr-xr-x 1 malik malik   94 Jan 22 11:13 greptest
-rw-r--r-- 1 malik malik    0 Jan 22 10:28 processedfiles
-rw-r--r-- 1 malik malik 2501 Jan 22 10:55 test.log
1 done ###

For some reason grep not working. I tested in three computers with Ubuntu 64 bit and 32 bit.
Any body know any other way I could do this?

Thank you,

---------- Post updated at 02:31 PM ---------- Previous update was at 11:18 AM ----------

Hello all I mange to fix the problem using find command. But still I do not know why grep failed. I found grep failed with any number. if I use characters work fine.

Basically I used the script to find last 3 days files in a folder and copy to another location.
using find command as below I mange to do the same and also that make my script smaller too
find /var/lct/mou2/processed/ -mtime -2 > /home/trans/mou/processedfiles
Code:
#!/bin/bash
LOGFILE=/home/malik/test/test.log
find /var/lct/mou2/processed/ -mtime -2 > /home/trans/mou/processedfiles
exit

Now all worked fine with cron and command line. using the content of the processedfiles I can copy the files.

Thank you for helping me

Last edited by pludi; 01-22-2010 at 02:29 AM.. Reason: code tags, please...
# 5  
Old 01-22-2010
The problem I see is that the date format in $TODAY and $YESTERDAY does not match the date format in your "ls" from cron. Maybe you have an alias for "ls" in the user acount which produces the non-standard date format?

In general the match string for grep should be in double quotes.

As you have deduced it is better to use "find" in this circumstance anyway.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern not working in Cron script

Hi, I have written a shell script to list all the files with some pattern as below. <CODE> ls *_20151201*.txt <CODE> its working properly when ran manually. But when i tried to run by cronning it.. its throwing an error that no files exists with file name *_20151201*.txt But when I try... (3 Replies)
Discussion started by: ssk250
3 Replies

2. Shell Programming and Scripting

Script (with sql queries) not working using cron

Hi all, I have script, which performing sql queries and put output into file. When I run this script manually, its working fine, but when I want to schedule it with cron I am getting errors... I defined LD_LYBRARY_PATH and ,but no result. After I defined it, I am getting error: # more... (4 Replies)
Discussion started by: nypreH
4 Replies

3. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

4. UNIX for Dummies Questions & Answers

Email Script not working when added to cron[solved]

Hi I have written an email script in python which sends email to the given id. I have customized the script for generating space alert inside a shell script as shown below df -h /microfocus > /tmp/spacereport ## Filter the %usage to variable per per=$(awk '{if (NR==3){print $4}}'... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

5. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

6. Shell Programming and Scripting

stdout redirect is working buy direct script exec but not in cron

Hi @ all :) i made a very little shell script witch is working well when i'm launching it directly like with ./script but when i'm launching it by cron tab it work at half only. the part of the script witch are not working are: #!/bin/sh apt-get updade apt-get -s upgrade >>... (5 Replies)
Discussion started by: calibal
5 Replies

7. Shell Programming and Scripting

Script not working when called by cron

Hello, I have the following script which works fine when ran from the command line: #!/apps/python/2.3.4/bin/python import os import sys import time user = os.getenv("USER") string = time.strftime("%m%d%y0000 " + user, time.gmtime()) However, when I have this run by crontab, I... (4 Replies)
Discussion started by: cooldude
4 Replies

8. Shell Programming and Scripting

sudo command is not working inside a script when placed in cron

Hi All, i have a cron entry like 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /amex/sssmonitor/dss_chk.ksh and the script is like #!/bin/ksh file=`uname -n` > /sunmast/projects/oasis/COREDEV/Dss$file.log > /tmp/output_sss today=`date` varb=`ps -ef | grep... (5 Replies)
Discussion started by: usha rao
5 Replies

9. Shell Programming and Scripting

running script in cron - with ssh commands - not working

I ran an ssh command to run a script on a remote server ssh -l <user> <servername> /path/to/script/scriptname This works fine - and the script is executed correctly. However - I put this command into a script, that I want to run from cron every hour, to execute the file on the remote... (31 Replies)
Discussion started by: frustrated1
31 Replies

10. Shell Programming and Scripting

script not working in CRON

guys i have written a very simple script .it runs manually well. but when i put it in cron,it doesn't give the desired output. script looks like this: #! /usr/bin/sh #script for loading data in table using ctl file/Abhijeet K/08.07.2006 /svm_wl1/. .profile cd... (5 Replies)
Discussion started by: abhijeetkul
5 Replies
Login or Register to Ask a Question