Reputation: 3351
How to keep golang project running even if console (putty) is closed. I have REST API developed in golang and hosted on AWS and using putty to connect and run the project
following command are used to install and run the project ( myapi )
go install myapi
myapi
when I close putty it stops working.
Upvotes: 1
Views: 1528
Reputation: 6079
You can use something like supervisord
Run your program as a non-privileged user and use the setcap utility to grant it the needed permissions.
For example, to allow binding to a low port number (like 80) run will need to run setcap once on the executable:
sudo setcap 'cap_net_bind_service=+ep' /opt/yourGoBinary
You may need to install setcap: sudo aptitude install libcap2-bin
Alternatively
Debian comes with a tool called start-stop-daemon
which is a standard way for starting daemons in init.d
scripts. It can also also put the process in background for you if the program does not do it on its own. Have a look at the --background
option.
Use /etc/init.d/skeleton
as the basis of your init script, but change the do_start()
function as follows:
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \
--background --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \
--background --exec $DAEMON -- $DAEMON_ARGS \
|| return 2
Also above added the --make-pidfile
option which creates the PID file for you.
In case you need to switch to a different user in a secure way, there is also --chuid
option.
On Ubuntu and RHEL/CentOS/SL 6.X the simplest way is to write an upstart
job configuration file. Just put exec /usr/sbin/yourprogram
in the /etc/init/yourprogram.conf
configuration file. With upstart there is no need to force the program in background. Do not add expect fork
or expect daemon
which you need with traditional daemons. With upstart it is better if the process does not fork.
Upvotes: 4
Reputation: 166
You have a number of options to keep your process running. The easiest of which is to use the nohup
command.
$ nohup myapi &
The above command should run your application and print the output to a file called nohup.out. This file will be located in the directory where you run the command. Another option is to use screen or tmux.
If you want to start running your project in a more production ready way, you should look into service managers like systemd.
Upvotes: 12