I used to run a Perl TCP Socket program 24*7 in my UBUNTU server. Usually I would open the Gnome terminal and go to script path and start the Perl script. The Gnome terminal stays open throughout. It was working fine for 6 months. But for the past three days, It is not working properly. Gnome terminal is closing automatically and so the Socket program. The next day morning I again opened the terminal and ran the socket program, then again the terminal has been closed overnight. I could not find any solution in the web. Need help in resolving the issue
3 Answers
Run your program in screen. Just install screen, open your terminal and type "screen". From that moment on everything you do will be inside of a persistent 'screen' that doesn't depend on whether the outside shell is closed.
If your terminal is closed, you can open a new one and re-attach the screen.
If I write programs that need to run like this (I will usually start them remotely through ssh) I usually write in a check, so that the program refuses to start unless it's running in screen (check if the environment variable STY exists). That way I can't accidentally run the program outside of screen.
-
-
2To reattach to an existing screen session, use
screen -r
. (Also,screen -x
lets you attach to a session that another terminal is already attached to) Sep 14, 2015 at 14:02 -
1
It is possible to start the program in background (your script might need some alteration if it is not possible) with ...
nohup {program} > {program.out} 2>&1 &
The output from program will be redirected to {program.out}. If you change the ">" to ">>" it will append the output instead of writing over the previous time it was used.
That would eliminate the need for a terminal session. Besides that it will also trap error messages you might have missed due to the terminal session being killed/stopped.
-
I can successfully run the script with background. But while killing the process the socket doen't get closed. If I apply the command sudo netstat -peanut | grep ':1008' I'm getting the PID. But if I try to kill PID it keeps changing and socket doesn't gets closed. Same doen't happens, while opening and closing with terminals. I might want to create a separate question for this matter, I think.– gzixSep 15, 2015 at 5:48
Press CTRL + ALT + F2 and sign in with your user-name and password.
Run the command from there and it shouldn't close.
-
Though this is the most simple and reliable technique, screen seems pretty cool to use.– gzixSep 25, 2015 at 12:25
nohup {command} &
) would do that. Or use a different ttty? By the way: Ubuntu probably does not close it out of itself. You probably need to find out why, it could be a crash or a problem with your perl programcron_daily
or anything else in/var/log
.dmesg
's output or the system logs?