<SOLVED> Cron won't run my bash script - newbie help needed!

Forum rules
Before you post please read this

<SOLVED> Cron won't run my bash script - newbie help needed!

Postby doogy64 on Sat Nov 05, 2011 5:07 am

Hi,

This is my first go at writing a bash script, which so far works fine when I execute it from the command prompt but I need some help getting cron to run it.

Thanks to Mr Google, I found and adapted a piece of code to get myscript.sh to do what I want, which is to open an app called camorama for 5 minutes (so it can take a series of still shots from a webcam 1 minute apart)

#!/bin/bash
# myscript.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin


echo "pkill -KILL -f 'camorama'" | at now + 5 minutes
camorama -M
echo "Goodbye Camorama"

[snipped other lines from script]

All is fine with the above in that it does what I want from the command line when I type "bash myscript.sh", however, I need it to be scheduled with cron. I created the following cron job (crontab -e) and tested it. I know the cron job is running and myscript.sh is invoked but for some reason (some permission thing?) the piece of code for camorama isn't doing anything.


# m h dom mon dow command
51 17 * * * /home/rich/myscript.sh
53 17 * * * echo "testcam" > /home/rich/output.txt

I have a feeling it's a permission thing, but I've exhausted my linux abilities! I'd appreciate any help.

Regards

Rich
Last edited by doogy64 on Sun Nov 13, 2011 7:13 pm, edited 1 time in total.
doogy64
Level 1
Level 1
 
Posts: 3
Joined: Fri Oct 28, 2011 4:36 am

Linux Mint is funded by ads and donations.
 

Re: Cron won't run my bash script - newbie help needed!

Postby xenopeek on Sat Nov 05, 2011 7:38 am

Test where camorama is located:
Code: Select all
which camorama

If that is not included in your script's PATH, give the full path to camorama to invoke it.

Also, please capture logging for any crontab scripts. You really won't know what is going wrong without logging :wink: You can easily do this, by inserting the following line after the #!/bin/bash line:
Code: Select all
exec >>/var/log/myscript.log 2>&1

This appends logging from stdout and stderr to the file /var/log/myscript.log (name it as you want, and of course you can choose a different path if you want).
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14660
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Cron won't run my bash script - newbie help needed!

Postby Anakinholland on Sat Nov 05, 2011 8:44 am

Vincent Vermeulen wrote:Also, please capture logging for any crontab scripts. You really won't know what is going wrong without logging :wink: You can easily do this, by inserting the following line after the #!/bin/bash line:
Code: Select all
exec >>/var/log/myscript.log 2>&1

This appends logging from stdout and stderr to the file /var/log/myscript.log (name it as you want, and of course you can choose a different path if you want).


Personally I would add it to the crontab:

Code: Select all
51 17 * * * /home/rich/myscript.sh >>/var/log/myscript.log 2>&1


This way the script can still be run with output in the shell when you run it manually :)

Also, and I'm not sure of this, you may want to run the script in the background, as you have scripts so close to eachother. There's only 2 minutes between the 2 cron-entries, but the script runs for 5 minutes?

Code: Select all
51 17 * * * /home/rich/myscript.sh & >>/var/log/myscript.log 2>&1


Have fun!

Anakin
Please Edit your initial post title and add [SOLVED] once your question is resolved.
User avatar
Anakinholland
Level 3
Level 3
 
Posts: 145
Joined: Sun Oct 30, 2011 8:09 am

[SOLVED] Re: Cron won't run my bash script - newbie help nee

Postby doogy64 on Sat Nov 05, 2011 10:41 pm

Hi thanks for the handy hints, I have solved it with a work-around. I wasn't able to figure out what was happening, so thought I'd try a replacement for Camorama called fswebcam, and it works a treat!!

thanks again
doogy64
Level 1
Level 1
 
Posts: 3
Joined: Fri Oct 28, 2011 4:36 am

Re: Cron won't run my bash script - newbie help needed!

Postby thaimann on Wed Nov 09, 2011 11:28 pm

My suspicion is that cron doesn't have as environment variables as Bash does in user mode. As you can see there is quite a difference!
From cron:

HOME=/home/terry
LOGNAME=terry
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/home/terry

from my bash command prompt:

terry@Perseus ~ $ printenv
ORBIT_SOCKETDIR=/tmp/orbit-terry
SSH_AGENT_PID=4180
TERM=xterm
SHELL=/bin/bash
XDG_SESSION_COOKIE=faabe31f223fbee95284fc2b00000006-1320893035.980537-905782913
WINDOWID=69206046
GNOME_KEYRING_CONTROL=/tmp/keyring-tF6Rlf
GTK_MODULES=canberra-gtk-module
USER=terry
SSH_AUTH_SOCK=/tmp/keyring-tF6Rlf/ssh
SESSION_MANAGER=local/Perseus:@/tmp/.ICE-unix/4147,unix/Perseus:/tmp/.ICE-unix/4147
USERNAME=terry
DEFAULTS_PATH=/usr/share/gconf/gnome.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg
PATH=/home/terry/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
DESKTOP_SESSION=gnome
PWD=/home/terry
GDM_KEYBOARD_LAYOUT=us
GNOME_KEYRING_PID=4128
LANG=en_US.UTF-8
GDM_LANG=en_US
MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path
UBUNTU_MENUPROXY=libappmenu.so
COMPIZ_CONFIG_PROFILE=ubuntu
GDMSESSION=gnome
SHLVL=1
HOME=/home/terry
LANGUAGE=en_US:en
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=terry
XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-OltKGLGTr6,guid=d26c4f94b6ecb542cb4d873d00001410
WINDOWPATH=8
DISPLAY=:0
COLORTERM=gnome-terminal
XAUTHORITY=/var/run/gdm/auth-for-terry-j9Ke7z/database
_=/usr/bin/printenv
thaimann
Level 1
Level 1
 
Posts: 30
Joined: Thu Aug 18, 2011 9:36 am

Re: Cron won't run my bash script - newbie help needed!

Postby subxaero on Sat Nov 12, 2011 8:11 am

thaimann wrote:My suspicion is that cron doesn't have as environment variables as Bash does in user mode. As you can see there is quite a difference! ....


In that case, how about adding your cron entry i.e.

0 0 * * * /bin/bash -lc <your script>
subxaero
Level 1
Level 1
 
Posts: 2
Joined: Sun Oct 23, 2011 2:52 am

Re: Cron won't run my bash script - newbie help needed!

Postby thaimann on Sat Nov 12, 2011 5:27 pm

Oh if you insist:

30 21 * * * printenv
thaimann
Level 1
Level 1
 
Posts: 30
Joined: Thu Aug 18, 2011 9:36 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest