LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2008, 03:24 PM   #1
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Rep: Reputation: 15
Trying to write a bash script and I'm stuck


What I want to do: check if Ruby is running. If not, start it.

Should be pretty simple.

What I got so far look like this:

Code:
if
ps -e|grep ruby|grep -v grep
then
echo "Nothing to do"
else
cd /var/www/ruby/pso
sudo ruby script/server -d
echo "Ruby started"
fi
But when it run I get a bunch of command not found errors and it doesn't do the if loop. It tries to do everything.

So if I got this right, grep will return exit code 0 so if will do the then part and exit.

If it is not running, grep will return a 1 and then run the command to start it.

What am I doing wrong there?

Thanks!
 
Old 04-15-2008, 03:48 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,334

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
I haven't tested this solution but I would try this:

Code:
ps -e|grep ruby|grep -v grep
if $?
then
echo "Nothing to do"
else
cd /var/www/ruby/pso
sudo ruby script/server -d
echo "Ruby started"
fi
---------------------
Steve Stites
 
Old 04-15-2008, 03:52 PM   #3
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
Thank!

Let me try it....

Got the following result:

4643 ? 00:00:00 ruby
./qa.sh: line 10: syntax error: unexpected end of file

So it did the ps|grep thing but didn't like the rest.
 
Old 04-15-2008, 04:04 PM   #4
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
The original script works out of the box for me. Can you post the errors you are getting?
 
Old 04-15-2008, 04:05 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...and please leave out the ancient "ps|grep|grep -v grep" mistake if you can use pgrep:
pgrep -f ruby >/dev/null 2>&1 || { cd /var/www/ruby/pso; sudo ruby script/server -d; echo "Ruby started"; }
 
Old 04-16-2008, 08:31 AM   #6
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tsg View Post
The original script works out of the box for me. Can you post the errors you are getting?
Sure thing.

If I run my original script, I see the following.

Code:
[root@linux /]# ./qa.sh
: command not found
 4643 ?        00:00:00 ruby
: command not founden
Nothing to do
: command not foundse
: No such file or directoryww/ruby/pso
ruby: No such file or directory -- script/server (LoadError)
Ruby started
: command not found
This is running on CentOS 5.1 if it help.
 
Old 04-16-2008, 08:50 AM   #7
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
...and please leave out the ancient "ps|grep|grep -v grep" mistake if you can use pgrep:
pgrep -f ruby >/dev/null 2>&1 || { cd /var/www/ruby/pso; sudo ruby script/server -d; echo "Ruby started"; }
Didn't know pgreg existed. I see that this could save some time.

Mind explaining the rest of the command?

I'm trying to see if I got everything right.

pgrep -f ruby >/dev/null 2>&1

This will run without displaying anything.

The commands between the {} will only be executed if the pgrep return an exit code 1, correct? Because of the ||?
 
Old 04-16-2008, 09:10 AM   #8
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
Yes!

That command with pgrep works! Thanks unSpwan!

One thing that is bugging me: works fine if I run it on the prompt but if I try to run it from a .sh file, I see the following error: ./qa.sh: line 2: syntax error: unexpected end of file.

Now I just need to tweak it a little bit as I plan to make this a cron job.
 
Old 04-16-2008, 09:29 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Yig View Post
works fine if I run it on the prompt but if I try to run it from a .sh file, I see the following error: ./qa.sh: line 2: syntax error: unexpected end of file.
Without knowing exaclty how your complete script looks like that's hard to see.


Make the script it look like this:
Code:
#!/bin/sh --
[ "$1" == "--debug" ] && set -vxe
pgrep -f ruby >/dev/null 2>&1 || { cd /var/www/ruby/pso; sudo ruby script/server -d || echo "server startup failed"; }
exit 0
Now you can reference the script in your crontab. If it still doesn't work you can use a crontab line like for example "*/10 * * * * /path/to/script.sh --debug 2>&1". This would run the script every ten minutes and because you provided one argument of "--debug" it internally sets the options verbose, show lines to be executed and exit-on-error. Because cron e-mails output the "2>&1" makes sure you get all of it in your mail to read. BTW because cron e-mails output I changed the echo part. You probably only want to know if it failed to restart becuase then you'll have to take action manually.
 
Old 04-16-2008, 09:40 AM   #10
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
My complete script has only one line:

Code:
pgrep -f ruby >/dev/null 2>&1 || { sudo ruby /var/www/ruby/pso/script/server -d; echo "Ruby started"; }
But it works fine when I run it from the prompt.

I'll try adding #!/bin/sh to see if it'll work.

Damn, not working.

Code:
-bash: ./qa.sh: /bin/sh^M: bad interpreter: No such file or directory
But if I type /bin/sh on the prompt, the prompt change to sh-3.1#

Last edited by Yig; 04-16-2008 at 09:42 AM. Reason: Added results....
 
Old 04-16-2008, 09:45 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Heh. The linefeeds are wrong. Try running 'dos2unix' on your script.
 
Old 04-16-2008, 09:54 AM   #12
Yig
Member
 
Registered: Aug 2005
Location: Montreal, Qc
Distribution: CentOs 4.5/5.0
Posts: 113

Original Poster
Rep: Reputation: 15
Ha!

I first wrote the file on my Win box and copied it over the Linux machine.

I just copied the command in a new file that I created and it it working fine now. That explain why I've been having problems.

Thanks again.
 
Old 04-16-2008, 10:04 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
NP. IIRC WinSCP can convert CR/LFs, else getting an editor that can "save as UNIX text" or use 'vi', open the file, type :se ff=unix, then save.
 
  


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
Trying to write a bash script christianunix Linux - Newbie 5 10-26-2007 05:30 PM
read write in bash script yhus Programming 6 09-18-2006 12:23 PM
Need to write a bash script imagineers7 Linux - General 5 05-09-2006 11:17 PM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 05:50 PM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 08:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 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
Open Source Consulting | Domain Registration