I'm running Flask on an AWS instance. My goal is to be able to have Flask running on its own, without me having to ssh into it and run
python app.py
Is there a way to have this command run every time the AWS instance itself reboots?
Yes there is a way to start the python script on reboot.
On linux you will find /etc/init.d directory. You will need to write your own init.d script and put it inside /etc/init.d directory,which will indeed start your python script. Ahh ! wait its not goning to be magic. Dont worry, there is fixed format of init.d script. Script contains some basic tasks like start(),stop(),reload() etc. Just add the code that you want to run on start in start() block.
Some reference link : https://bash.cyberciti.biz/guide//etc/init.d
Try this:
(crontab -l 2>/dev/null; echo '#reboot python /path/to/app.py') | crontab -
Related
I am reading data from Google spreadsheet by using python script, the script is working fine when I am running using the shell command but it has not worked when i scheduled it on cron.
so far in check list that i have done
file name and location related no issue available
by using tail -f /var/log/syslog | grep CRON this command i have seen that schedule is fired on time
I have added my script method in a different script which is running fine from cron but still that method not worked in times of cron running
cron statement
/home/ubuntu/.virtualenvs/python_3.5/bin/python /location/script_name.py
Is there any other way to check this why it is not working from cron schedule but working fine from shell command.
After getting the suggestion from #stovfl i added >> /tmp/cron.log 2>&1 this line
/home/ubuntu/.virtualenvs/python_3.5/bin/python /location/script_name.py >> /tmp/cron.log 2>&1
with my cron statement and check the log file and i found i have not used explict path for authentication json file though it is inside project folder so times of shell command it it was working fine. used code just like below
credentials = ServiceAccountCredentials.from_json_keyfile_name('file_name.json')
and therefore when cron fired the scrip it failed to get the authetication file location and not ran.
then i changed like below
credentials = ServiceAccountCredentials.from_json_keyfile_name('location_of_file/file_name.json')
now it works like a charm from cron and shell command
I need to write a script in python which stops a service running in EC2 instance just before it reboots.
What should be the approach here?
Suppose service name is abc.
to manually stop this service, I execute:
service abc stop
I want to automate this before instance goes for reboot.
Help me with this
The cron scheduler allows you to use #reboot in place of a schedule string. So, write your script and then crontab -e and add the following:
#reboot /path/to/python /path/to/reboot_script.py
For the reboot script, python isn't really your best choice. I would write it using a bash script, looking something like this:
#!/bin/bash
service abc stop
If you must use python, you could use invoke:
from invoke import run
run('service abc stop', hide=True, warn=True)
Edit:
To run this BEFORE reboot, make a shell script called K99kill_service and add it to your /etc/rc6.d directory. These scripts are executed before reboot or shutdown. It's important that it is named as above to make sure it runs at the right time and doesn't interfere with other shutdown scripts.
Courtesy : https://opensource.com/life/16/11/running-commands-shutdown-linux
See the link, it explains in very much detail on, How to shutdown services in linux before shutdown for both SysVinit and Systemd ?
I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.
I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.
I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?
Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..
Anyway here is a detailed guide for future reference (Using Debian Stretch):
Make your script an executable
chmod +x myscript.py
Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here
nohup /path/to/script/myscript.py &
Logout from the shell if you want
logout
Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:
ps -e | grep myscript.py
I'm looking to create a script in python that initiates an SSH session with a server. I know it has to be a simple process i'm just not sure where to start. My ultimate plan is to automate this script to run on startup. i am not even sure python is the best way to go i just know it comes preloaded on raspbain for the pi.
A simple bash script would be better suited to the task. It is possible with python, but there's no obvious reason to make it harder than necessary.
From
write a shell script to ssh to a remote machine and execute commands:
#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done
From how do i run a script at start up (askubuntu):
You will need root privileges for any the following. To get root, open
a terminal and run the command
sudo su
and the command prompt will change to '#' indicating that the terminal
session has root privileges.
Alternative #1. Add an initscript.
Create a new script in /etc/init.d/myscript.
vi /etc/init.d/myscript
(Obviously it doesn't have to be called "myscript".) In this script,
do whatever you want to do. Perhaps just run the script you mentioned.
#!/bin/sh
/path/to/my/script.sh
Make it executable.
chmod ugo+x /etc/init.d/myscript
Configure the init system to run this script at startup.
update-rc.d myscript defaults
Alternative #2. Add commands to /etc/rc.local
vi /etc/rc.local
with content like the following.
# This script is executed at the end of each multiuser runlevel
/path/to/my/script.sh || exit 1 # Added by me
exit 0
Alternative #3. Add an Upstart job.
Create /etc/init/myjob.conf
vi /etc/init/myjob.conf
with content like the following
description "my job"
start on startup
task
exec /path/to/my/script.sh
Depending on what you do with the ssh connection, if it needs to stay open over the entire up time of the device, you will need to use some more trickery however (ssh connections are autoclosed after a period of inactivity).
I have 3-4 python scripts which I want to run continuously on a remote server(which i am accessing through ssh login).The script might crash due to exceptions(handling all the exceptions is not practical now), and in such cases i want to immediately restart the script.
What i have done till now.
I have written a .conf file in /etc/init
chdir /home/user/Desktop/myFolder
exec python myFile.py
respawn
This seemed to work fine for about 4 hours and then it stopped working and i could not start the .conf file.
Suggest changes to this or i am also open to a new approach
Easiest way to do it - run in infinite bash loop in screen. Also it's the worst way to do it:
screen -S sessionName bash -c 'while true; python myFile.py ; done'
You can also use http://supervisord.org/ or daemon by writing init.d script for it http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts
If your script is running on an Ubuntu machine you have the very convenient Upstart, http://upstart.ubuntu.com/
Upstart does a great job running services on boot and respawning a process that died.