Running python script in background EC2 - linux

I have a code.py script that I want to run on my Linux EC2 instance. I want it to run even when the tab of the EC2 instance in my browser is closed, so that the code is running in the background.
Is there any way to do this?

this command run your python file on another screen that called YourScreenName.
screen -dmS YourScreenName python3 code.py
if you close EC2 instance tab on your browser, this screen will be running on your server.
for access to this screen you can use this command.
screen -r YourScreenName
and for quit from this screen use Ctrl + a + d

Related

puppeteer doesnt work in nohup mode but works normaly

Ive made a node js web scraper with puppeteer.
I want it to run 24/7 so I tried running it on a aws ec2 virtual machine.
if I run the program normally "npm start" it works perfectly, but beacause I want to close the ssh connection and go about my day, Ive tried running "nohup npm start &" but when I run it like that I either get an error in 'nohup.out':
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
or just the code stops running and nothing happens
ended up just using
ctrl + z
bg
disown -a
instead

How to load a docker image using tar.gz file in google compute engine?

I am trying to run the following command on google compute engine instance the tar.gz file is large 24GB, and I am using 8 cores 32GB CE with 100GB storage. Whenever i run the following command my prompt get's stuck(It gives no output/no standard output which is very annoying) and i have no ways to know what's happening with command. I did run the same command on my localhost and successfully spin up the container. I am not sure how long to wait as there is no feedback in std out.
docker load -i cnt7-jarvis-cdh63.tar.gz
Try using screen to run this process in background:
screen
docker load -i cnt7-jarvis-cdh63.tar.gz
Then detach (do not press Enter after these keys):
Ctrl+A
d
And check resource usage:
top
After process has completed you can go back to that screen:
screen -ls
screen -r <NUMBER>

AWS EC2: keep running node.js after exit console

I use aws ec2 to host web server with node.js and apache.
So far, I always need to login ec2 through terminal to run npm start.
I wanna make it keep running even if Im not on terminal. How am I supposed to setup for it?
I turned on https, but nothing happened.
I appreciate it in advance.
One option is using the screen command:
screen
You will reach a new bash prompt for that screen. Run your app.
To detach the screen, press Ctrl+A, then D to detach from that screen.
To reattach screen when you next SSH in, use
screen -ls
to see detached screens and use
screen -r xxxxx
with the screen number to reattach that screen.

run an app and switch to it via ssh?

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).

Start a server running with screen in a vagrant box

I'm trying to run a server automatically when my vagrant box boots.
Similar to start screen detached in a vagrant box with ssh, how?, except I'm trying to do it with a provisioning script set to run: "always".
I'm doing something like this: nohup screen -S server -mL -d bash -c 'start-my-server.sh'.
The server starts fine, and if I would have done this within the shell, I could switch to the server with screen -r server.
When I go in after with vagrant ssh, it doesn't find any screens...I'm assuming this is because its not the same shell session.
Is there anyway to get a hold of that screen session?
Edit
Forgot to mention that I had prefixed the screen command with nohup
The answer is that vagrant provision was running as a privileged user, and therefore I couldn't see the screen logging in as the vagrant user.

Resources