LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 01-27-2011, 01:36 AM   #1
joesto
LQ Newbie
 
Registered: Jun 2010
Location: liverpool GB
Distribution: ubuntu 10.4
Posts: 18

Rep: Reputation: 0
Question how to run a program in script


hi all
i have a program called teamviewer and i want to get it to start if it stops
in terminal is i type teamviewer it starts the program
but my script is not working any ideas



#!/bin/sh
#Script to check if the teamviewer are running.
#Script is written by joesto1
process=`ps auxwww | grep teamviewer | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo "Couldn't find teamviewer running Restarting teamviewer "`date` >>/var/cccamlog/team.check
teamviewer
else echo "teamviewer is still OK! "`date` >>/var/cccamlog/team.check
fi

i am using ubuntu 10.04
thanks

joe
 
Old 01-27-2011, 02:26 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
I assume you're not running that script manually, but that you are calling it through a cron job.

A script executed by cron typically does not have the same PATH environment variable as you do when you're typing in commands at a terminal. I would strongly suggest that you use the full path to the teamviewer program to make it easier on yourself.

If you don't know the full path, you can find out by executing this at a terminal:
Code:
which teamviewer
 
Old 01-27-2011, 02:36 AM   #3
prodev05
Member
 
Registered: Jul 2009
Location: Planet Earth
Distribution: Unix & Linux Variants
Posts: 304

Rep: Reputation: 20
Thumbs up

#!/bin/sh
#Script to check if the teamviewer are running.
#Script is written by joesto1
process=`ps auxwww | grep teamviewer | grep -v grep | awk '{print $1}'`
if [ -z "$process" ];
then
echo "Couldn't find teamviewer running Restarting teamviewer "`date` >>/var/cccamlog/team.check
echo "`Enter teamviewer full path` " >> /var/cccamlog/team.check
else
echo "teamviewer is still OK! "`date` >>/var/cccamlog/team.check
fi
exit 0
---------------------

Should give the command with full path within back-quote (` /usr/bin/command`). Just try. Good luck.
 
Old 01-27-2011, 12:13 PM   #4
joesto
LQ Newbie
 
Registered: Jun 2010
Location: liverpool GB
Distribution: ubuntu 10.4
Posts: 18

Original Poster
Rep: Reputation: 0
Thumbs up

that to you both for the help

yes i am using a crontab to activate the script
when i use the command which teamviewer it returns

/usr/bin/teamviewer

so have changed in the

#!/bin/sh
#Script to check if the teamviewer are running.
#Script is written by joesto1
process=`ps auxwww | grep teamviewer | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo "Couldn't find teamviewer running Restarting teamviewer "`date` >>/var/cccamlog/team.check
`/usr/bin/teamviewer`
else echo "teamviewer is still OK! "`date` >>/var/cccamlog/team.check
fi

but still log udated but teamviewer not start
any thing else i can try
thanks
joe
 
Old 01-27-2011, 02:09 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Wrap code in CODE tags and use indentations in conditional statements, it makes it a lot easier to read. Use capital letters and punctuation and read what you have typed to make sure it makes sense, it makes it a lot easier to read. ("that to you both...")

The reason teamviewer doesn't launch is probably because you've used


Code:
`/usr/bin/teamviewer`
when it should be


Code:
/usr/bin/teamviewer
Instead of 'ps auxwww' try using pidof.

Code:
#!/bin/bash
#Script to check if the teamviewer are running.
#Script is written by joesto1

app="/usr/bin/teamviewer";

if [ -z "$(pidof $app)" ]; then
   echo "Couldn't find $app running Restarting $app $(date)" >>/var/cccamlog/team.check;
   # use & and disown otherwise this script will keep running. This is assuming that $app does not automatically 
   # background itself.
   $app & disown
else
   echo "$app is still OK! $(date)" >>/var/cccamlog/team.check;
fi
 
1 members found this post helpful.
Old 01-27-2011, 03:48 PM   #6
joesto
LQ Newbie
 
Registered: Jun 2010
Location: liverpool GB
Distribution: ubuntu 10.4
Posts: 18

Original Poster
Rep: Reputation: 0
Thumbs down

thanks for the script the log still works to a point in that it does not work if teamviewer is running it puts

Couldn't find /usr/bin/teamviewer running Restarting /usr/bin/teamviewer Thu Jan 27 20:46:01 GMT 2011 on to the log file even thought teamvierwer is working
and still does not restart it if it is stopped
thanks
joe


ps
if i run your script from terminal it restarts teamviwer but not from crontab

Last edited by joesto; 01-27-2011 at 03:52 PM. Reason: update
 
Old 01-27-2011, 04:02 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Alternative
Code:
#!/bin/bash
#Script to check if the teamviewer are running.
#Script is written by joesto1

app="/usr/bin/teamviewer";
app_name=$(basename $app)

if [ $(ps -ef |$app_name|grep -v grep |wc -l) -eq 0 ]; then
   echo "Couldn't find $app running Restarting $app $(date)" >>/var/cccamlog/team.check;
   # use & and disown otherwise this script will keep running. This is assuming that $app does not automatically 
   # background itself.
   $app & disown
else
   echo "$app is still OK! $(date)" >>/var/cccamlog/team.check;
fi
EDIT:

If a script runs manually but not from cron that suggests that one of the components is not in the PATH environment variable cron.

To resolve this you need to use the full path for commands.

To find the path of a command from a shell type:

type command

example:

type basename

Last edited by Disillusionist; 01-27-2011 at 04:07 PM.
 
1 members found this post helpful.
Old 01-31-2011, 12:41 PM   #8
joesto
LQ Newbie
 
Registered: Jun 2010
Location: liverpool GB
Distribution: ubuntu 10.4
Posts: 18

Original Poster
Rep: Reputation: 0
thanks disillusionist

tryed your alternatime script still no joy
the log update ok
but it say that teamviewer is running even when it is not
thanks
joe
 
Old 02-02-2011, 02:53 AM   #9
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
If my script reports the application as being up even when it's down,
and arizonagroovejet's script states correctly whether the application is running.

We can consider that /usr/bin is not in your PATH environment variable when running from cron, as both basename and wc live in /usr/bin

However this does not tell us why that application is not restarting.

Are we sure that the application is not closing when the cron task finishes? I know that is what disown is supposed to prevent, but we could check that it was started using:
Code:
#!/bin/bash
#Script to check if the teamviewer are running.
#Script is written by joesto1

app="/usr/bin/teamviewer";

if [ -z "$(pidof $app)" ]; then
   echo "Couldn't find $app running Restarting $app $(date)" >>/var/cccamlog/team.check;
   # use & and disown otherwise this script will keep running. This is assuming that $app does not automatically 
   # background itself.
   $app & disown
   if [ -z "$(pidof $app)" ]; then
      echo "Failed to restart $app" >> /var/cccamlog/team.check;
   fi
else
   echo "$app is still OK! $(date)" >>/var/cccamlog/team.check;
fi
 
Old 02-02-2011, 11:56 AM   #10
joesto
LQ Newbie
 
Registered: Jun 2010
Location: liverpool GB
Distribution: ubuntu 10.4
Posts: 18

Original Poster
Rep: Reputation: 0
Thumbs up

thanks for you time m8's

Disillusionist
i run your new script fron crontab and it did not start teamviewer and put this in the log file

Couldn't find /usr/bin/teamviewer running Restarting /usr/bin/teamviewer Wed Feb 2 16:48:01 GMT 2011
Failed to restart /usr/bin/teamviewer

i run your script from terminal and it started teamviewer and logged this

Couldn't find /usr/bin/teamviewer running Restarting /usr/bin/teamviewer Wed Feb 2 16:48:44 GMT 2011
Failed to restart /usr/bin/teamviewer

hope this helps
i dont know if thias is relivent but if i run script in terminal as root it comms back saying that teamviewer cannot be run as root
forgive me if this is nonsence as i am a new learner
thanks
joe
 
  


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
[SOLVED] Run a C program from bash script bvkim Programming 5 05-10-2010 01:35 AM
Run a script from a C program? shilpa rangappa Programming 14 01-12-2010 08:54 AM
Run a program from a c script crashovaride Linux - Software 3 11-12-2006 10:41 AM
Script to run a program bluedude2288 Programming 3 06-11-2004 06:13 PM
run shell script in c program u4u Linux - General 1 02-14-2004 03:48 PM


All times are GMT -5. The time now is 06:30 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration