I have used various snippets of code to build a system which
listens to a port for incoming TCP data (using a perl script), writes this data to a log file.
calls and runs a PHP script to consume the log file and write it to an RDS MySQL DB
I have a GPS device configured to send the data to the elastic IP of my AWS EC2 Server
It works fine, and when i run via SSH
perl portlistener.pl
it does it's job fine, happily working away.
The only way I can stop the script running is by closing the terminal window, ending my SSH session. What I need to do, is keep it running at all times, and to implement a start, stop and restart facility. Do i need to create a daemon?
I know PHP, but until now have never worked with Perl. I'm also not that familiar with command line, other than installing updates, navigating and editing single files etc.
Thanks in advance for any help, or for pointing me in the right direction.
Solved it I think!
installed CPAN http://www.thegeekstuff.com/2008/09/how-to-install-perl-modules-manually-and-using-cpan-command/
Using CPAN, installed Deamon::Control
Then created a new program as below (portlistener_launcher.pl), and ran it as SU.
#!/usr/bin/perl
use strict;
use warnings;
use Daemon::Control;
$ENV{PHP_FCGI_CHILDREN} = 10;
$ENV{PHP_FCGI_MAX_REQUESTS} = 1000;
Daemon::Control->new({
name => 'portlistener',
program => 'perl /home/ec2-user/portlistener/portlistener.pl',
fork => 2,
pid_file => '/var/run/portlistener.pid',
stdout_file => '/var/log/portlistener.log',
stderr_file => '/var/log/portlistener.log',
})->run;
There's probably a neater way of doing it, but it seems to work, and I can stop/start it like so:
perl portlistener_launcher.pl start
If the terminal window is the only task, you can use the nohup command, e.g.
http://linux.101hacks.com/unix/nohup-command/
To terminate the listener you can kill an appropriate running process or processes.
An implementation of daemon does not ensure its permanent running. It can crash or might be killed from someone. To guarantee the permanent daemon running you must implement a 24x7 monitoring of this daemon and automatic restarting of it.
Related
I have a demo project in AWS and then I created an AMI for it so that I can use it for auto-scaling. now I am looking for something that I can put in user text in my launch configuration which will let me start the server without going to ssh. I am trying out below, let me know where is my mistake.
#!/bin/bash
cd demo
node server.js
when I launch a new instance with my AMI and just do cd through SSH it works absolutely fine, however, I want to start the server with going to SSH.
These are common one can face when running node application without process manager on a remote server.
Let suppose the above script but what if a node application encounter error? so the application will be stopped, so better to use process manager which will take care of such thing and you will not need to do ssh.
You can use pm2. Which also have slack integration another interesting feature that will help to monitor the process.
You can also set Setup startup script.
Restarting PM2 with the processes you manage on server boot/reboot is
critical. To solve this, just run this command to generate an active
startup script:
run these command in the AMI, and pm2 will take care of the process on all instances.
pm2 startup
#And to freeze a process list for automatic respawn:
pm2 save
I'm currently running a python script as a systemd service. Is there any way to "switch" into the service and take control of the script? The script has a menu but runs calculations in the background in another thread. Most likely not, so is there a way to run a python script 24/7, start on boot, restart on crash etc (just like systemd service) but be able to take control of it after I connect to the server via SSH, so I can manipulate the app?
One solution you could try would be to edit the systemd configuration to launch the process in screen or tmux, then attach that when logging in via SSH.
For instance, in the systemd unit, you might have:
[Service]
Type=single
ExecStart=tmux new "command"
Using Type=single will treat the tmux command as the main process, which would be killed if you stop it with systemctl stop systemprocess
The ExecStart=tmux new "command" creates a new tmux session with the command inside of it.
You can then attach to it using tmux attach as the same user the systemd unit is running as (I believe this is root by default).
We are finishing development of a project, the client is already using it but occasionally some errors occur - crashing the server.
I know I could register a service as 'upstart' script on linux, in order to have my node service restart when it crashes.
But our server is running other stuff, so we can't restart it.
Well, actually, while writing, I realize I have two questions then:
Will 'upstart' work without having to reboot? Something is just whispering yes to me :)
If not, what other option would I have to 'respawn' my node server when it crashes?
Yes, upstart will restart your process without a reboot.
Also, you should look into forever.
PM2 is a Production process manager for Node.js app.
If your focus for automatic restart is an always running application, I suggest to use a process manager. Process manager, in general, handles the node process(es if cluster enabled), and is responsible for the process/es execution. PM leans on the operative system: your node app and the OS are not so strinctly chained because the pm is in the middle.Final trick: put the process manager on upstart. Here is a complete performance improvement path to follow.
Using a shared server and not having root privileges, I can't download or install any of the previously mentioned libraries. What I am able to do is use a simple infinite bash loop to solve my issue. First, I created the file ./startup.sh in the base directory ($ vim startup.sh):
#!/bin/bash
while:
do
node ./dist/sophisticatedPrimate/server/main.js
done
Then I run it with:
$ bash startup.sh
and it works fine. There is a downside to this, which is that is doesn't have a graceful way to end the loop (at least not once I exit the server). What I ended up doing is simply finding the process with:
$ ps aux | grep startup.sh
Then killing it with
$ kill <process id>
example
$ kill 555555
Can we hook to similar start,stop etc events. Do we have to write them as shell scripts? I know of mono port of .NET.
You are looking for something called an 'init script'. These are scripts that allow you to start or stop a service with a single command, like so:
service httpd restart
service httpd stop
service httpd start
Some Linux distributions do not include the service command, in which case you access init scripts directly by their location, /etc/init.d, like so.
/etc/init.d/mysqld restart
You can program your init script to accept whatever parameters you want (start, stop, restart, etc). Some basic tutorials on writing init scripts to get you started can be found at the following web pages:
http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html
http://www.linuxquestions.org/questions/programming-9/how-to-write-init-script-376302/
Many times an init script is unnecessary, and you can just go with the simpler option of executing your program in the background and killing it manually. Running an executable on Linux in the background can be done like so:
./some_prog arg1 arg2 &
And killing it is done like this:
kill `pgrep some_prog`
If you are fairly new to Linux, that latter option might be a much easier way to go until you get a handle on init scripts and the general Linux service ecosystem.
I have ssh-ed to a remote machine. I have enabled X11 forwarding (ssh -X) and I have started a GUI program.
The program takes 5 minutes to set up to do some processing but the actual processing takes 1-2 hours. I don't want to stream data while the program is working. I don't even want to see the program again as it produces files as output when it finishes and I can just scp them.
So how can I quit the ssh session but leave the program running on the machine? CRTL+C in the terminal?
EDIT: For the answer please see my comment just below.
Long story short - you can't do this without making some modifications to the way you run things. A GUI application requires for an X server target to be accepting it's GUI updates - if you're using your local X server to do this, then it'll require (1) the connection to be open (2) that you actually handle the updates.
A way around this is to use something like Xvfb - which is a headless way of hosting a virtual X-server. Above and beyond the examples provided on the wikipedia page, folks who seem to make frequent use of this mechanism are Selenium users.
Awesome, I've been looking for an acceptable answer to my problem for hours, and finally one pops up. ssh -X -f login#machine yourprogram worked perfectly for me. Though I used ssh -Y -f login#machine yourprogram. This has been driving me nuts.
Like some people said, SSH -X is using your local X server, so it needs the connection. I had the same problem, wanted to quit ssh but leaving GUI applications running. To do this I installed X server and VNC server on the remote host. With a VNC client on your local computer, you can easily connect to the VNC server and disconnect leaving GUI applications running.
By the way, you will have better performances with VNC or X2Go. In my case, Firefox was very slow and some sites didn't load at all with SSH -X, even with -Y or -C optimizations.
Running ssh -X -f login#machine yourprogram should do the trick.
Starting your program with nohup program & will make it safe to just close your terminal - program will still be running.
You won't be able to see the UI after you end ssh session, but since you don't need it anyway - it's going to do the job.