Check if program is running, if not start it and enter a password... easy, isn't it?
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
Check if program is running, if not start it and enter a password... easy, isn't it?
Hello,
I just installed socks5-rev26 to my xubuntu.
In order to start it, i need to execute those commands:
Code:
cd socks5-rev26/bin
./socks5 socks5.conf
"type a password"
I wish instead that this is done automatically after boot, and that once a while it checks if socks5 is running, else start it.
I probably can use something like http://www.anyexample.com/linux_bsd/...ell_script.xml that to create a script that check if the program is still running, and add it to crontab. But once i found it's not running anymore, how can i start it and let it enter the password?
Any example would be really appreciated.
In an expect script you can tell it what the password prompt looks like then have it type the password for you.
For example this expect script is one I use to reboot a fibre bridge here:
Code:
!/usr/local/bin/expect -f
log_file -a expect.out
set ip [lrange $argv 0 0]
spawn telnet $ip
expect "Configuration Login:"
send bridgeuser\r
expect "Password:"
send bridgepassword\r
expect "4) Reboot"
expect "Command >"
send 4
expect "Reboot! Are you sure (y/n)?"
send y
sleep 5
In the above each "expect" line is text I'm waiting to see. Once I see it then I "send" a response. bridgeuser and bridgepassword are the (obfuscated) user and password I would login with.
The argv takes input from command line so I execute above expect script with the IP address at command line so it knows which device to telnet into.
In your case you're not having to telnet. You just need to work out the expected prompt and put that in an expect line then do the send yourpassword\r.
I actually use the above to reboot 10 fibre bridges so have a shell script that calls the above 10 times (once for each IP).
One thing to remember - there are pauses - when you see prompts on the screen you'll be tempted to do input or hit enter - that will interfere with the expect script - just let it do its thing.
Another thing to remember - insure your cron script has the necessary paths to all commands you'll use. The cron job won't use the same environment you do when you test from command line.
Note: The first line is where expect is on one of my servers - it may be elsewhere on yours (you may even need to install expect). Be sure to edit the first line so it has correct location.
Finally: The expect script stores the password in clear text so it is important you set permissions on it so that only the user that runs it and/or root can read the file.
No because you've made the above a shell script by making its first line the #!/bin/sh.
What you really need is to make a shell script that calls the expect script. (Alternatively you may be able to embed an expect function within the shell script but I've not done that.)
So shell script would be:
Code:
#!/bin/sh
SERVICE='sock5'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
socks5.expect
fi
Of course as I noted before you need to include paths since you're doing this in cron:
e.g. instead of "ps ax" you need to do "/bin/ps ax"
instead of just "grep" you'd need "/bin/grep"
instead of just socks5.expect you'd need "/pathto/socks5.expect".
Really appreciated! I will try that as soon as i come back home.
and to add it to the crontab, i just need to type "crontab -e" and add "01,16,31,46 * * * * /pathto/shellscript" so that the check is performed every 15 mins? And is there a way to execute it with my user priviledge and not as root? Because when i start it manually, if i run it with my user, it works good, if i run it as root (with sudo ./socks ...) it doesn't work properly.
thanks very much
Pretty much - make sure you test from command line before adding to cron. On occasion expect requires an extra timeout or carriage return (\r) to work properly.
The crontab entry looks fine to me but I'd add logging to it - the cronlog only has start success/failure - not output from the cron scripts:
To make it run as your user instead of root you just run the crontab -e as yourself instead of as root (or as root add the -u flag to specify user). Any user can run a cron command as themselves. It will automatically create the cron file for your user the first time you add an entry.
ok i tried to launch the script ./socks5.script after chmodding it +x. it gave me error, so i chmodded +x socks5.expect too. Now it's executed, but doesn't run as expected. The log produced is:
Code:
sock5 is not running
spawn /home/ciccio/socks5-rev26/bin/./socks5 socks5.conf.e
Enter config blowfish key:
and then exit.. i do ps ax | grep socks5 and it's not started..
if i run the socks5 program manually, the expected behaviour is:
Code:
ciccio@ubuntu:~/socks5-rev26/bin$ ./socks5 socks5.conf.e
Enter config blowfish key:
using default value '0' for use_ssl
using default value '' for ssl_cert
using default value '0' for no_chroot
[BIND - 2530,Wed Jul 16 22:35:02 2008] [Bind] bind to any adr
[-GETIP- - 2530,Wed Jul 16 22:35:02 2008] try to get ip for: www.xxx.com
[-GETIP- - 2530,Wed Jul 16 22:35:02 2008] resolved ip: xxx.xxxx.xxx.xxx
[-SYSTEM- - 2530,Wed Jul 16 22:35:02 2008] - WARNING: - Could not chroot
[-SYSTEM- - 2530,Wed Jul 16 22:35:02 2008] - WARNING: - Could not set uid!
[ACCEPT - 2530,Wed Jul 16 22:35:02 2008] [Accept] start
and then the program waits for socks connections..
What's wrong with the script? I tried to add a pause before sending the password, but nothing changes.. Maybe the script, when finishes, close the started application instead of running it in background?
Thanks
You're not understanding expect properly. You have to tell it literally what to expect.
In what you wrote in prior post you had:
Code:
expect "Password:"
However in the output it is clear that the socks command never prompts the literal string:
Quote:
Password:
Instead it prompts the literal string:
Quote:
Enter config blowfish key:
That means what you are "expecting" is the above literal string. So your statement should be:
Code:
expect "Enter config blowfish key:"
There may be other things you "expect" to occur before or after that. The key is that the expect statement isn't expecting some general thing - it is expecting specifically what you would see on the screen when you were doing it manually.
yeah my bad, i didn't say that i changed "password" to "key", but it still doesn't work..
To be more precise, when I run the program through a shell, i can't do anything else since it's not ran in background. so when i close the shell or i lost the connection with my server box, socks5 just terminate. I don't run it with & because i wouldn't be able to insert the password.
That's why i want to crontab it and, sometimes, check if it runs...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.