LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Script Not Working in Cron (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-not-working-in-cron-788129/)

3rods 02-09-2010 10:16 PM

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.

evo2 02-09-2010 10:28 PM

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.

catkin 02-09-2010 11:06 PM

Does the script or the commands within it write anything to the terminal?

Davno 02-10-2010 02:59 AM

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. :)

colucix 02-10-2010 03:28 AM

Quote:

Originally Posted by 3rods (Post 3858539)
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

:twocents:

3rods 02-10-2010 11:38 AM

[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!

evo2 02-10-2010 08:24 PM

Quote:

Originally Posted by 3rods (Post 3859265)
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.


All times are GMT -5. The time now is 02:15 AM.