Register
It is currently Thu Oct 30, 2014 7:43 am

New to Bashscripting..advice on script if service is running


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Mon Aug 19, 2013 4:14 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Hello,

I'm new to bashscripting and would like to learn how to solve for checking if rdesktop is running on my Debian Workstation. If it's not running I will then connect to the virtual machine through the same bash script.

So far I've come up with the following as a starter:

#!/bin/bash
ps -A | grep rdesktop | awk '{print $1}'
if [ -z "$1" ]; then
rdesktop 192.168.1.x -u administrator -f
fi

This would assume I have awk installed. is there a way to to this check without using awk?

Thank you,


Top
 Profile  
 PostPosted: Mon Aug 19, 2013 5:31 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
I've done some more work on this script and discovered that it actually isn't working for me. Even if rdestkop is not connected to a virtual machine, my grep command is still finding old rdesktop pid's?

I had tried to clear the $1 variable but then discovered multiple rdesktop pid's.

Here is my modified code:

#!/bin/bash
ps -A | grep rdesktop | awk '{print $1}'
if [ -z "$1" ]; then
echo $1
unset $1
else
rdesktop 192.168.1.x -u administrator -f
unset $1
fi

How would I find only active rdesktop connections?

Thank you.


Top
 Profile  
 PostPosted: Mon Aug 19, 2013 6:07 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
I just noticed your 'need a script' forum. Should I move this post to that forum instead?

Thank you.


Top
 Profile  
 PostPosted: Mon Aug 19, 2013 7:13 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

$1 in awk is not the same that $1 in bash.
in awk, $1 is the first field
in bash it's the first script's argument.

what's the output of
Code:
ps -ef -C rdesktop
and what line(s) do(es) matter ?

if all lines matter, simply
Code:
if ps -ef -C rdesktop 2>&1 > /dev/null
then
   echo "rdesktop is already running"
else
   rdesktop 192.168.1.x -u administrator -f
fi
you could also use ``pidof'' instead.


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 8:36 am   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Hello,

I've tried using ps -ef as suggested to find if rdesktop is running...but then I happened upon the following script which is working for me:

Code:
#!/bin/sh
# Check if rdesktop is running
if pgrep rdesktop > /dev/null
then
    #echo "Running"
    exit 0
else
    #echo "Stopped"
    rdesktop 192.168.100.100 -u Administrator
    exit 0
fi


I've made my script executable and the the script will startup my rdesktop session when I double click on the script.

What I'd like to do now is run this script from a cron job every 10 minutes or so and have my rdesktop session reconnected. From my user command (not root) I've used crontab -e to add the following:

*/10 * * * * cd ~/scripts && check.sh>>~/scripts/check.log

But my script is not executing. Any ideas what I've done wrong?

Thank you.


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 9:21 am   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
/path/to/rdesktop 192.168.100.100 -u Administrator


Top
 Profile WWW  
 PostPosted: Wed Sep 04, 2013 9:40 am   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Thanks for the reply.

I've done some digging and found that the problem was in my crontab setup. I needed to add DISPLAY to crontab to execute a GUI app. Here is my crontab entry to run my script every minute.

* * * * * export DISPLAY=:0 && cd ~/scripts && sh connect.sh>>~/scripts/check.log

And I've made a few more changes to my script to add better logging. Here is the final script:

Code:
#!/bin/sh
# Check if rdesktop is running
LOGFILE=~/scripts/check.log
if pgrep rdesktop > /dev/null
then
    echo "$(date "+%m%d%Y %T") : rdesktop is already running." >> $LOGFILE 2>&1
    exit 0
else
    echo "$(date "+%m%d%Y %T") : rdesktop has Stopped.  Attempting to restart now." >> $LOGFILE 2>&1
    rdesktop 192.168.100.100 -u Administrator
    exit 0
fi


Thanks!


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 9:42 am   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Is there a way to mark a post as resolved?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP