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
<SOLVED> Cron won't run my bash script - newbie help needed!
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
<SOLVED> Cron won't run my bash script - newbie help needed!
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Cron won't run my bash script - newbie help needed!
Test where camorama is located:
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 You can easily do this, by inserting the following line after the #!/bin/bash line:
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).
Code: Select all
which camorama
Also, please capture logging for any crontab scripts. You really won't know what is going wrong without logging 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
Re: Cron won't run my bash script - newbie help needed!
Personally I would add it to the crontab:xenopeek wrote:Also, please capture logging for any crontab scripts. You really won't know what is going wrong without logging You can easily do this, by inserting the following line after the #!/bin/bash line: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).Code: Select all
exec >>/var/log/myscript.log 2>&1
Code: Select all
51 17 * * * /home/rich/myscript.sh >>/var/log/myscript.log 2>&1
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
Anakin
[SOLVED] Re: Cron won't run my bash script - newbie help nee
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
thanks again
Re: Cron won't run my bash script - newbie help needed!
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
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
Re: Cron won't run my bash script - newbie help needed!
In that case, how about adding your cron entry i.e.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! ....
0 0 * * * /bin/bash -lc <your script>
Re: Cron won't run my bash script - newbie help needed!
Oh if you insist:
30 21 * * * printenv
30 21 * * * printenv