LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 09-25-2012, 03:42 PM   #1
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
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?

pgrep -u raj -f '.*alltray.*evolution' > /tmp/evolutionPgrep.pid
 
Old 09-25-2012, 04:20 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,540

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
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.
 
1 members found this post helpful.
Old 09-25-2012, 04:52 PM   #3
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Original Poster
Rep: Reputation: Disabled
Code:
 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.

 
Old 09-25-2012, 05:25 PM   #4
leosubhadeep
Member
 
Registered: Jul 2012
Location: West Bengal,India
Distribution: Ubuntu 14.04,Fedora 20,Windows 7
Posts: 111
Blog Entries: 3

Rep: Reputation: 1
Is the process running and the user id is right? Use the bash command
Code:
top
while the process is running to see all processes with pid in realtime.
This should snap your process. Check there and run the command again.
 
Old 09-25-2012, 05:27 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,304
Blog Entries: 54

Rep: Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855
Quote:
Originally Posted by Rupadhya View Post
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
 
1 members found this post helpful.
Old 09-26-2012, 12:05 AM   #6
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Original Poster
Rep: Reputation: Disabled
[Solved] It finally works! Here is a copy of it!

I finally got it to work..
Code:
#!/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!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command works when pasted at command line but not as bash script neild Programming 7 09-23-2012 07:30 AM
[SOLVED] echoing a command line within a bash script? stf92 Programming 2 07-08-2011 05:57 AM
[SOLVED] Command line works, won't work from inside bash script w6lsn Programming 4 02-17-2011 05:45 AM
bash script with own command line interpreter Juako Programming 2 01-19-2011 04:47 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM


All times are GMT -5. The time now is 01:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration