EvilZone

Programming and Scripting => Scripting Languages => : m0l0ko September 14, 2013, 02:03:49 AM

: How to create a daemon
: m0l0ko September 14, 2013, 02:03:49 AM
Don't know if daemon is the right word, I want to make a program that will always be running in the background, waiting for something to happen. In this case I want it to wait for me to get disconnected from my VPN, then it will reconnect me. Basically, I need it to make sure I am always connected to a VPN. What kinda programming concepts do I need to know for this? Can this be done with bash scripting? If not, I suppose I'll use java.
: Re: How to create a daemon
: Kulverstukas September 14, 2013, 08:25:07 AM
I'm not expert on this, but java is not the right tool to use. I'd recommend C++, but try bash first.
: Re: How to create a daemon
: Deque September 14, 2013, 10:33:37 AM
Have a look here: http://www.gabsoftware.com/tips/automatically-reconnect-to-your-vpn-on-linux/ (http://www.gabsoftware.com/tips/automatically-reconnect-to-your-vpn-on-linux/)

They do it entirely with bash and also explain how to run it automatically after login in the background.
It is no special programming concept, just check for a variable and sleep for 30 seconds, then check again and sleep for 30 seconds.
: Re: How to create a daemon
: vezzy September 14, 2013, 04:34:38 PM
I'd recommend C++, but try bash first.

C++ for a daemon?

Anything beyond C is overkill, especially if he's in a *nix environment.
: Re: How to create a daemon
: m0l0ko September 14, 2013, 08:43:57 PM
Have a look here: http://www.gabsoftware.com/tips/automatically-reconnect-to-your-vpn-on-linux/ (http://www.gabsoftware.com/tips/automatically-reconnect-to-your-vpn-on-linux/)

They do it entirely with bash and also explain how to run it automatically after login in the background.
It is no special programming concept, just check for a variable and sleep for 30 seconds, then check again and sleep for 30 seconds.

Thanks. I didn't think of just putting it on a sleep loop. Does adding the script to your .profile directory make it run as a background process?
: Re: How to create a daemon
: Deque September 14, 2013, 09:58:59 PM
Thanks. I didn't think of just putting it on a sleep loop. Does adding the script to your .profile directory make it run as a background process?

No. That just starts it after you logged in. The & in the end of the command makes it work in the background.
: Re: How to create a daemon
: m0l0ko September 20, 2013, 03:02:43 AM
It works. Thanks a lot. Now I'm trying to think of a way to switch it off and on when I need to. I tried making an environment variable VPN_SWITCH and set it to either on or off, but the script can't read environment variables that I set in another instance of bash like that.
: Re: How to create a daemon
: Deque September 20, 2013, 09:21:11 AM
Just shut down the program or start it again?
: Re: How to create a daemon
: m0l0ko September 22, 2013, 12:23:41 AM
I can't seem to kill the process:
horse@box:~$ ps -e | grep 'vpn'
 2405 ?        00:00:00 vpn.sh <defunct>
horse@box:~$ kill 2405
horse@box:~$ ps -e | grep 'vpn'
 2405 ?        00:00:00 vpn.sh <defunct>

BTW why does it say <defunct> beside the process name?
: Re: How to create a daemon
: proxx September 22, 2013, 10:22:38 AM
I can't seem to kill the process:
BTW why does it say <defunct> beside the process name?

Its a so called 'zombie' , it probably finished but is still around :)
: Re: How to create a daemon
: Deque September 23, 2013, 08:55:03 PM
Have you tried kill -9 ?