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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
doogy64

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

Post by doogy64 »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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).
Image
Anakinholland

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

Post by Anakinholland »

xenopeek 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
doogy64

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

Post by doogy64 »

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
thaimann

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

Post by thaimann »

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
subxaero

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

Post by subxaero »

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>
thaimann

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

Post by thaimann »

Oh if you insist:

30 21 * * * printenv
Locked

Return to “Scripts & Bash”