I have a node.js server and redis I need to start via ssh for a simple web app to start on a remote server I setup. I tried using tmux to connect to the server and start the two processes and then I detach the process. At this point I can still connect to the website. I then restart my local machine I was ssh’ing into my server and then I can no longer connect to my web app. Would anybody know how I can start the process on Ubunut server and disconnect and for the process not to stop?
Something like nohup command &
should work. Where command is whatever command you use to start the service.
i use to have a little shell script in rc.local on ubuntu server that would execute screen commands to get things like MC servers up and running but also leave them detached so i could just come back later if needed. there are probably better ways of doing it but thats how i used to do it. for node.js too you can use something like PM2 and have it help you with handing what node.js apps need to be running on startup.
You can either:
- Daemonize node.js using Forever
- Use disown
$ ./app > app.log 2>app.err &
[1] 1234
$ disown %%
- Use
nohup ./app
- but you won't be able to shut it down gracefully withkill
.
my solution on my server is just to use screen though i have a separete user account for every server.
just run screen and then run whatever program you want to run. If you lose the ssh connection then just run "screen -d -r" will drop you right where you left off
EX:
ssh [email protected]
$ screen
$ sever-ssh.sh
SSH connection to host.com dropped
ssh [email protected]
$ screen -d -r
you could write a .service file and use systemd to control it. you will need the program to change user to a nonprivileged account once started.
[Unit]
Description=your description here
After=network.target
[Service]
ExecStart=full path to your command here
[Install]
WantedBy=multi-user.target
next
systemctl enable your_name.service
this will run it on startup
then
systemctl start your_name
other options : stop restart
info taken from here
more info on systemd
run as another user:
change
ExecStart=full path to your command here
to
ExecStart=/bin/su user -c full path to your command here
note : /bin/su might be a different path on ubuntu than specified above
for node js stuff i like pm2
sudo npm install -g pm2
then
pm2 start "your app"
then just use
pm2 startup systemd
if you have any trouble here's a guide
scroll down to the pm2 part near the bottom
I have used this, and you can name screen sessions for easier re-attachment of different sessions later.
Thanks your solution worked the best!!!
I'm not sure what your asking but you can create a permanent ssh tunnel between a web app and a remote database. You can use the following after setting up keys. With some debugging/modifying.
CreateTunnel() {
/usr/bin/ssh -f -N -L13306:hostb:3306 -L19922:hostb:22 tunnel@hostb
if [[ $? -eq 0 ]]; then
echo Tunnel to hostb created successfully
else
echo An error occurred creating a tunnel to hostb RC was $?
fi
}
## Run the 'ls' command remotely. If it returns non-zero, then create a new connection
/usr/bin/ssh -p 19922 tunnel@localhost ls
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
Yes, I stole that from the internet, and no you don't need most of that crap, just check out the ssh command.
Hopefully that provides an option for you.
nano /etc/init.d/<> && ln -s /etc/init.d/<> /etc/rc5.d
put some script here, just make sure it includes the start/stop/etc cases, you can google examples of these easily.
This would start which ever scripting you put inside the start tag when ever the OS hits runlevel 5.
it may take a try or two but once you get the hang of it, it really makes sense.
also you can create your own status, restart, etc. cases.