Question

I have a VPS server with Bluehost(fedora OS), I wanted to capture data from remote application via TCP and to write the same onto a file for further processing. So I am using netcat for that.

 nc -l -k -v IP_ADDRESS_HERE 5331 > /home/user/public_html/tcp/data.xml

by using above command in ssh, the Netcat listens to a particular port and writes a captured data on to a file called data.xml.

The Problem: I want this command to run continuously even after closing ssh terminal, and even after server is restarted. I used to screen and nohup. These options not going to be the solution as it won't run if the server is restarted.

How do i do that? using some scripts?, I am bit new to this field, please help me.

Thanks in advance :)

Update: Even in ssh terminal, above command works perfect but automatically ends with ^C after some time. please help me.

Was it helpful?

Solution

If the job needs to resume after system reboot, it's a system service. So you have several options:

  • Write an init script and add it to your default run level.
  • If your distro uses systemd write a configuration file to start your command as a service.
  • You can use another service manager like supervisord to automatically run your command.
  • Custom quick and dirty solution. When I need something to run automatically in a screen, I often use a cron entry like this :

    @reboot screen -S my_service -d -m bash -c 'my_command' > /dev/null

At reboot, it will start your command in a screen and detach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top