LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2010, 11:16 PM   #1
3rods
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 16
Question Shell Script Not Working in Cron


Ok, here is the script. The idea is that it checks a directory for jpeg files, gets the file names, writes the names to a text file, moves the jpeg files to another directory (for sanitation later), and inserts the filenames in to a mysql database.

Code:
#!/bin/bash

ls -l | awk '{print $8}' | grep .jpg >> output.txt


if [ ! -s output.txt ]
	then
		rm output.txt
        	exit 0
	fi

while read -r LINE ; 

	do mv "$LINE" ../img/"$LINE" ; done < output.txt


mysqlimport -u username --password=password --local -c filename databasename output.txt


rm output.txt

exit 0
The file itself is running in /var/www/temp.
I have added the following lines to my crontab (tried in user and root crontab):

Code:
# m h  dom mon dow   command
SHELL=/bin/bash
PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

#*/1 * * * * /var/www/temp/img_prep
img_prep is the script filename.


I think it has something to do with either my path (the script needs to know where it is relative to the files it needs to work with/find) or it has to do with crontab not having the right environment variable or path.

I feel like it's something silly or bush league, but I don't know what.

Any suggentions would be appreciated. It works correctly when I run it from the command line.

Last edited by 3rods; 02-09-2010 at 11:17 PM. Reason: typo
 
Old 02-09-2010, 11:28 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
I'm no expert at cron scripts but I guess it is because of the location from which the script is being run. Try putting the following at the start of the script:

Code:
cd $(dirname $0)
On a side note, I'd replace your "ls -l" line with
Code:
echo *.jpg > output.txt
Evo2.
 
Old 02-10-2010, 12:06 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Does the script or the commands within it write anything to the terminal?
 
Old 02-10-2010, 03:59 AM   #4
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 231

Rep: Reputation: 25
Like Evo2, I am no script expert either but I have a few script running and they all start with cd to the proper directory.
Does your script work manually (not by cron)?
Also you should create a directory to store your script and add it to your path in /home/<username>/bin and also make the script executable with chmod 775 /home/<username>/bin/YourScriptName. Then if it work nice manually, it will work with cron.
And sorry if all this is stuff you already know.

Last edited by Davno; 02-10-2010 at 04:03 AM.
 
Old 02-10-2010, 04:28 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by 3rods View Post
I think it has something to do with either my path (the script needs to know where it is relative to the files it needs to work with/find) or it has to do with crontab not having the right environment variable or path.
That's correct. To avoid annoying surprises (and maybe a lot of headache) you should take the usage of full (absolute) paths in the cron scripts as an habit. Just for example, I should modify your script as:
Code:
#!/bin/bash
ls /path/to/*.jpg >> /path/to/output.txt

if [ ! -s /path/to/output.txt ]
	then
		rm /path/to/output.txt
        	exit 0
	fi

while read -r LINE

	do mv "$LINE" /path/to/img/"$(basename $LINE)" ; done < /path/to/output.txt

/path/to/mysqlimport -u username --password=password --local -c filename /path/to/databasename /path/to/output.txt

rm /path/to/output.txt

exit 0
 
Old 02-10-2010, 12:38 PM   #6
3rods
Member
 
Registered: Mar 2008
Posts: 70

Original Poster
Rep: Reputation: 16
Thumbs up [solved]

Ok, so, this is what I ended up with. I ended up using two separate files; one to move, one to upload in to the database. For the DB I needed filename only, but the mv needed to be absolute - so two files.

I'm open to suggestions on cleaner code:
Code:
#!/bin/bash

> /var/www/temp/out.txt
> /var/www/temp/gallery.txt

for i in /var/www/temp/*;

        do echo $i | grep .jpg >> /var/www/temp/out.txt; done


if [ ! -s /var/www/temp/out.txt ]
	then
		rm /var/www/temp/out.txt
		rm /var/www/temp/gallery.txt
        	exit 0
	fi

while read -r LINE ; 

	do mv "$LINE" "/var/www/img/$(basename "$LINE")"; done < /var/www/temp/out.txt

while read -r LINE; 
	
	do echo $(basename "$LINE") >> /var/www/temp/gallery.txt; done < /var/www/temp/out.txt

mysqlimport -u user --password=password --local -c filename ci_database /var/www/temp/gallery.txt


rm /var/www/temp/gallery.txt
rm /var/www/temp/out.txt

exit 0
It wasn't handling special characters and spaces in filenames correctly, so I had to make some slight mods to some of the code suggestions.

Thanks everyone!
 
Old 02-10-2010, 09:24 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Quote:
Originally Posted by 3rods View Post
It wasn't handling special characters and spaces in filenames correctly, so I had to make some slight mods to some of the code suggestions.
My suggestions in post #2 were to fix relative path and spaces in filenames. Did they not work?

Evo2.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script works different from cron necrolin Programming 2 05-07-2009 10:52 AM
Help requires for my first cron/shell script mwi.user Linux - Server 10 08-12-2008 12:14 PM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
Cron wont run my shell script shelb Linux - Desktop 4 05-27-2007 10:55 PM
run shell script on cron varunbihani Linux - Newbie 5 07-08-2005 02:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration