Why does this work from the bash command line and then fails in a bash script?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why does this work from the bash command line and then fails in a bash script?
I am trying to capture the Process ID of the running evolution running under ALLTRAY. This works in the command line, but creates an empty file in a bash script. Any ideas?
use the code flag and paste the script you are getting errors in. also run the script with sh -xvv and post that output. that will go a long way towards helping us help you.
sh -xvv ./evobackup.sh raj /home/raj/c/backup/raj/2012_09_25/evolution-backup
#!/bin/bash
# Backup for Evolution 3.4.4
# it can be used in USER's cronjobs
# with full pathes given
if [ -z $2 -o -d $2 ]; then
echo "Usage: [/path/to/]evobackup.sh {username} [/path/to/]backupfile --quiet"
exit 1
fi
+ '[' -z /home/raj/c/backup/raj/2012_09_25/evolution-backup -o -d /home/raj/c/backup/raj/2012_09_25/evolution-backup ']'
# remember evolutions running state for current user
running=0
+ running=0
USER=$1
+ USER=raj
HOME=/home/$USER
+ HOME=/home/raj
pgrep -u $USER -f '.*alltray.*evolution' > /tmp/evolutionPgrep.pid
+ pgrep -u raj -f '.*alltray.*evolution'
sed 's/ *//' | sed 's/[^0-9].*//' /tmp/evolutonPgrep.pid > evolution.pid
+ sed 's/ *//'
+ sed 's/[^0-9].*//' /tmp/evolutonPgrep.pid
sed: can't read /tmp/evolutonPgrep.pid: No such file or directory
Looks like the redirect to evolutionPgrep.pid didn't work.
Looks like the redirect to evolutionPgrep.pid didn't work.
Not if you write "/tmp/evolutonPgrep.pid" w/o "i" ;-p
*BTW, if this script is supposed to be run by unprivileged users from their crontab then there's some things you should not or should not need to do like try to set variable names in use by the system like NAME and USER. Just check if the UID isn't 0, test for existence of ${LOGNAME}'s ${HOME} and then check for existence of Evolution running before you test for existence of and backup ${LOGNAME}/.evolution, ${LOGNAME}/.local/share/evolution or whatever places it's in these days.
Last edited by unSpawn; 09-25-2012 at 05:46 PM.
Reason: //More *is* more
#!/bin/bash
# Backup for Evolution 3.4.4
# it can be used in USER's cronjobs
# with full paths given
if [ -z $2 -o -d $2 ]; then
echo "Usage: [/path/to/]evobackup.sh {username} [/path/to/]backupfile --quiet"
exit 1
fi
# remember evolutions running state for current user
running=0
username=$1
quiet=$3
homeDirectory=/home/$username
echoQuiet () {
if [ -z "$1" ] # Is parameter #1 zero length?
then
return
else
if [ .$quiet. != ".--quiet." ]; then
echo "$1"
fi
fi
return
}
pgrep -u $username -f '.*alltray.*evolution' > /tmp/evolutionPgrep.pid
checkPgrep=$?
echoQuiet "This is the check of pgrep $checkPgrep"
sed 's/ *//' /tmp/evolutionPgrep.pid | sed 's/[^0-9].*//' | cat > /tmp/evolution.pid
if [ $checkPgrep -eq 0 ]; then
running=2
else
pgrep -u $username -f evolution >/tmp/evolutionPgrep.pid
checkPgrep=$?
sed 's/ *//' /tmp/evolutionPgrep.pid | sed 's/[^0-9].*//' | cat > /tmp/evolution.pid
if [ $checkPgrep -eq 0 ]; then
running=1
fi
fi
echoQuiet "evobackup: running=$running"
# shutdown before doing a backup
if [ $running -gt 0 ]; then
evolution --quit
sleep 5
fi
exec < /tmp/evolution.pid
value=0
while read line
do
value=`expr $value + 1`;
echoQuiet "Checking for processes and killing it: $line";
kill -9 $line
done
echoQuiet "Killed the evolution program."
cd $homeDirectory
if [ -d .config/evolution -a -d .local/share/evolution ]; then
echo -e "[dirs]\ndata=.local/share/evolution\nconfig=.config/evolution\n"\
> evolution.dir
[ -f .local/share/evolution/.running ] && \
rm .local/share/evolution/.running
gconftool-2 --dump /apps/evolution > \
.local/share/evolution/backup-restore-gconf.xml
if [ -d $homeDirectory/.camel_certs ]; then
tar -chzf "${2%%.*}"_`date -I`.tar.gz evolution.dir \
.local/share/evolution .config/evolution .camel_certs
else
tar -chzf "${2%%.*}"_`date -I`.tar.gz evolution.dir \
.local/share/evolution .config/evolution
fi
else
echoQuiet "evolution dirs not found. Exiting."
exit 1
fi
echoQuiet "evobackup: tar exit code $?"
rm .local/share/evolution/backup-restore-gconf.xml
#rm -f evolution.dir
# in case we'll running a GUI program from cron,
# we must give it a display
ROOT_UID=0 # Root has $UID 0.
if [ "$UID" -eq "$ROOT_UID" ] # Will the real "root" please stand up?
then
echo "You are root." >/dev/null
else
[ -z $DISPLAY ] && export DISPLAY=:0.0
[ $running -eq 1 ] && /usr/bin/evolution &
[ $running -eq 2 ] && /usr/bin/alltray -- /usr/bin/evolution &
fi
exit
I have tested it from root's crontab, which will allow me to backup all the users evolution automatically. I changed the script to use $homeDirectory instead of HOME and $username instead of USER so I don't conflict with the built in functions.
It seems to work pretty well. Thanks for the help!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.