LinuxQuestions.org
Help answer threads with 0 replies.
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 06-27-2013, 01:45 PM   #1
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Rep: Reputation: 13
Check if a program is running, if it isn't start it?


I have a program set to autostart within my Desktop Environment using the autostart program. Occasionally the computer doesn't start this program.

Manually, I do:
Code:
ps -e | grep program
If nothing is returned, it's not running, and so I just start it with:
Code:
program &
Can I get the computer to do this automatically? The program in question is Deluge.

Thanks
 
Old 06-27-2013, 01:51 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
yes you would put that into a small BASH script and place it into cron to run every other min or so. depending on how often you want it to run.

Code:
#!/bin/bash
first line of code 
test
new code
exit
make the script executable:

Code:
chmod +x foo
then in your crontab -e (that is e for edit)

Code:
*/2  *  *  *  *  /path/to/script
thats all there is to it.

Last edited by lleb; 06-27-2013 at 01:55 PM.
 
1 members found this post helpful.
Old 06-27-2013, 01:56 PM   #3
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Original Poster
Rep: Reputation: 13
Thanks, now I can narrow down my search a little bit
 
Old 06-27-2013, 02:02 PM   #4
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Original Poster
Rep: Reputation: 13
Having copied and pasted bits of random lines from the internet, can anyone tell me if this looks suitable and sensible?

Code:
#!/bin/bash
ps -C "deluge" >& /dev/null
if [[ $? -eq 0 ]]; then
    deluge
else exit
fi
 
Old 06-27-2013, 02:32 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
$? is the exit code of the last process, in your scripts case ps
an exit of 0 means there was no error
anything greater than zero is an error

So at the moment your if statement is true if deluge is running and false if not.
The opposite of what you want.
two ways to fix that


Code:
if [[ $? ! -eq 0 ]]; then
here ! inverts the result, true becomes false and vice versa.

Or

Code:
if [[ $? -gt 0 ]]; then
-gt is greater than


always a good idea to test exit codes, I've come across things in the past which always exit 0 even if there was an error ( flashimage on Android )

Code:
ps -C "deluge" >& /dev/null ; echo $?
ps -C "FoodelugeBar" >& /dev/null ; echo $?

you should also look at http://wiki.bash-hackers.org/syntax/keywords/coproc

your command(s) will 'detach' from the shell, so for example you could close the terminal without killing what you had started.

Also means your script will exit instead of waiting for your command to finish


Personally I would have else and exit on separate lines
Code:
#!/bin/bash
ps -C "deluge" >& /dev/null
# copy'n'paste fail
#if [[ $? -eq 0 ]]; then
if [[ $? ! -eq 0 ]]; then
    deluge
    SomeCmd1
    SomeCmd2
else
    command1 
    command2
    com...
    exit
fi
That white space makes it easier to see the separation

Edit: forgot
here you don't need the else, as you are not really doing anything else
Code:
#!/bin/bash
ps -C "deluge" >& /dev/null
# copy'n'paste fail
#if [[ $? -eq 0 ]]; then
if [[ $? ! -eq 0 ]]; then
    deluge
fi
and it can be simplified further
Code:
pidof deluge || deluge
see
http://tldp.org/LDP/abs/html/testconstructs.html#EX11
[/code]


BUT, to be honest you should really look at why deluge is not starting/crashing

Last edited by Firerat; 06-29-2013 at 02:19 PM. Reason: Fix Copy'n'paste fails,
 
1 members found this post helpful.
Old 06-27-2013, 02:41 PM   #6
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Some notes that may be of use to the OP going forward:

"ps -e | grep program_name" should work in your specific case here, but you cannot trust "ps -ef | grep program_name". Why would you want to add the -f option to ps? Because in the generic case, you might not be grepping for the program_name only, but you may be grepping for something more specific.

Using "ps -ef | grep program_name" can fail, because it will not only match "program_name", but it will also match "grep program_name". So the grep may see itself grep'ing, and thus your script would think that program_name is running when actually it is not. It's a timing issue whether grep will catch itself or not, but it certainly can, and usually does, so you need to take that possibility into account.

Better to use something like "pkill -0 -f program_name" to check if it's running or not.

Or, "ps -ef program_name | grep -v grep" would produce a similar result, by filtering out any instance of grep catching itself.

Say for example, you wanted to check if somebody was using vi to edit a specific file. You could not say "ps -e | grep vi" because that would find every instance of vi and you are only interested in the specific instance of "vi specific_filename". So you would have to use something more specific like ps -ef | egrep "vi .*specific_filename" | grep -v egrep (or the pkill -f equivalent). Even that regular expression in the egrep command may not be specific enough for this contrived example, but I'm not doing a tutorial on regular expressions, I'm only trying to point out that grep may catch itself grep'ing.

Last edited by haertig; 06-27-2013 at 02:43 PM.
 
1 members found this post helpful.
Old 06-29-2013, 12:34 PM   #7
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Original Poster
Rep: Reputation: 13
This isn't working at the moment, I'd like to check a few basic things. As I understand it, this should run my script twice an hour.
Code:
* */2 * * * /home/user/.scripts/script
The following is the script it's pointed to, which is executable.
Code:
#!/bin/bash
ps -e "deluge" >& /dev/null
if [[ $? -eq 0 ]]; then
    deluge
fi
Thanks
 
Old 06-29-2013, 12:39 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
im not 100% on your cron entry. if you want to run 2x every hour id set it as:

Code:
*/30 * * * * /home/user/.scripts/script
that will run at the top of the hour and the 30min mark.
 
1 members found this post helpful.
Old 06-29-2013, 01:32 PM   #9
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Original Poster
Rep: Reputation: 13
Thank you. I've adjusted that, so I believe it should have run twice now, but still this program isn't started.

I tried running the script manually, and alas, I think that is where the problem lies. Is there a way to see what the script is doing and might be failing?

Last edited by NotAComputerGuy; 06-29-2013 at 01:40 PM.
 
Old 06-29-2013, 02:09 PM   #10
Z038
Member
 
Registered: Jan 2006
Distribution: Slackware
Posts: 835

Rep: Reputation: 165Reputation: 165
The command you are executing is not valid. It will never work. Try it without redirecting the output to /dev/null, and you'll see. It is always going to return an exit status of 1, so your test for equal to 0 will never match. And when it returns a 1, it does not mean what your script wants it to mean.

This is NOT what you want to do!
Code:
ps -e "deluge"
What works is to use -C (command name) instead of -e. The -C option takes an argument, the -e option does not.

Code:
ps -C "deluge"
Issue it from a command line like this to see the exit code for a command name that exists and one that doesn't.

Code:
ps -C "deluge" >& /dev/null ; echo $?
However, what you want to test for is an exit status greater than 0, or you can test for exit status equal 1. Firerat told you this already. If you get an exit status of 0, it means that ps found the command you passed as an argument on the -C switch. If you get an exit status of 1, it means that the command was not found. It is when it is NOT found that you want to start it.

You should really go back and read what Firerat and haertig told you. Firerat explained the exit status test problem you had, and haertig pointed out the inconsistent results you can get with the ps command. Use one of the alternative techniques that one or the other of them mentioned.

Last edited by Z038; 06-29-2013 at 02:13 PM.
 
1 members found this post helpful.
Old 06-29-2013, 02:23 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Just noticed I did a copy'n'paste fail, where I didn't put the invert in
Fix in the original post ( marked # copy'n'paste fail )
 
1 members found this post helpful.
Old 06-29-2013, 03:22 PM   #12
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 349

Original Poster
Rep: Reputation: 13
Due to a large amount of input from you guys, I have managed to get the script working correctly. Thank you!

I am just waiting for the half hour marker to pass to see if the cronjob is working, but to be quite frank, that will lmost certainly be for a different thread. Cronjobs have many guides to it, so a little reading and tinkering should see me right.
 
  


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] My Visual basics program isn't running properly, Why? errigour Programming 3 06-25-2012 03:38 PM
Bash: Check if a program is running naimslim89 Programming 12 05-08-2012 11:24 PM
Shell Script to start a program either not running or kill if running avataratar Linux - Newbie 1 03-29-2012 03:45 AM
Check if program is running, if not start it and enter a password... easy, isn't it? darkrad Linux - Server 11 07-17-2008 01:35 PM
Check if program is running Conjurer Programming 8 11-17-2005 02:37 PM


All times are GMT -5. The time now is 11:51 PM.

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