ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
...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"; }
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
...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 ||?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.