snick
Posts: 37
Joined: Sat Nov 24, 2012 4:30 pm

Cron vs CMD line process not excecuting

Sun Nov 25, 2012 10:20 am

Hi, wasnt sure if this qualified as Power - but here goes.
Also I have spent a long time trying to get this work, and searched S3cmd forums etc

Anyhoo - I am trying to setup the pi as a drop and leave upload machine - plug it in, turn it on and it will scan a folder for files - upload them to Amazon S3 if so - I have all parts of the process working - I am writing a log file on start and moving it when the job is done to prevent from Cron starting a new process while the file exists etc.
This works, but I cannot get S3cmd to work from Cron - works fine when calling the script via ./myscript on CLI.
Have tried putting the full path /usr/bin/s3cmd in the script, calling a new /root/.s3cfg file so that cron (if running as root) can access the key information etc.
But I just cannot get it to work:

* * * * * /media/USBflash/s3rcron_test
is my crontab entry (media/USBflash is a thumbdrive vfat format)

s3cron_test contains:
#!/bin/sh

if [ -f /media/USBflash/log.log ]
then
echo "still running" | tee /media/USBflash/log.log

else
echo "starting job `date +%m%d-%H%M`" >> /media/USBflash/log.log
/usr/bin/s3cmd put --confgure /root/.s3cfg —-include '*.mp4' --guess-mime-ty$
#rm /media/USBflash/log.log
mv /media/USBflash/log.log /media/USBflash/logs/`date +%m%d%H%M`.log
mv /media/USBflash/*.mp4 /media/USBflash/done/
fi
I tried with/without the --configure option but no difference.

But it will not execute - no output from s3cmd I can find even if I log the output.
Just cant seem to figure it out. as I say calling the script from CLI works fine.
So I'm wondering if its some Pi related configuration that I've missed.

Thanks

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Cron vs CMD line process not excecuting

Sun Nov 25, 2012 6:11 pm

You might try setting the environment variables within the script for the cron job, to be the same as yours when you are running from command line. I have had to do that with scripts on my system.

But it is on an Embedded Linux system, so not sure I can give you appropriate examples.

From the command line, you type "env" and it will display your current environment.

A good bet the script is running as a different user.

So, you also can type the following on the command line as well as enter it as well as enter it as the first command in your cron job:

from console: echo You are user $USER

in your script: echo You are user $USER > ~/cronuser.txt

I routed the echo output from your cron job to a file in your home directory, you can change that to any location you want. Another thought would be you might need to preface your commands in the cron script with sudo, it all is likely to be either a matter of permissions or of file paths differing if you used relative rather than absolute paths in the cron script.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

snick
Posts: 37
Joined: Sat Nov 24, 2012 4:30 pm

Re: Cron vs CMD line process not excecuting

Sun Nov 25, 2012 9:24 pm

Interesting, I tried what you suggested you are user $USER but the file only contains the words "you are user" then blank. Which seems like there must be something up?
I also tried a sudo i the script, but got the same (not processing) result.

Is there a way to force cron to run as a specific user? That way I can ensure the variables are accessible.


Thanks for your help.

N

ecw
Posts: 10
Joined: Tue Jan 10, 2012 1:56 pm

Re: Cron vs CMD line process not excecuting

Sun Nov 25, 2012 9:47 pm

5Cron will run jobs for any user that has been authorised to submit jobs - listed in /etc/cron.allow. You cannot rely on an environment at all like one has under an interactive login. Any env vars needed by cron jobs must be set explicitly, either on the job entry in your crontab, or by a wrapper.

Check the man pages -
man cron crontab

snick
Posts: 37
Joined: Sat Nov 24, 2012 4:30 pm

Re: Cron vs CMD line process not excecuting

Sun Nov 25, 2012 9:54 pm

Actually putting sudo in the script DID work.
Thanks SO much, took me a age to get this going, now I have an awesome upload box I can leave anywhere there's a network without worrying about loss or damage (at least not too expensive to replace.)

N

Return to “Advanced users”