django appscheduler cron job is not running into cpanel - python

Currently, I'm using a Django library named appscheduler.
First code:
job = scheduler.add_job(schedule_job, 'cron', day_of_week='mon-fri', hour=14, minute=5)
When I run this cron job in my localhost it's working very smoothly. But my Cpanel hosting not working. But in my Cpanel terminal if I run this code "python manage.py runserver" it is working until the terminal is open. When I close the terminal, it's not working.
The server opened into the terminal.
Another thing is that,
Second code:
job = scheduler.add_job(schedule_job, 'interval', seconds=50)
This interval job is running fine into localhost and also cpnael (N.B without running terminal it's working smoothly into Cpanel)
I couldn't detect here why the first code is not working without Cpanel and why the second code is working fine without terminal.

Related

how to make your python web app always running on ubuntu machine

I have a web app that I deployed to a machine that has ubuntu 20 installed
to be able to run the app I should open ssh to the ubuntu machine and then run this command
cd mywebapp
python3 app.py
it works successfully, but once I close the ssh console or reboot the machine or anything happens, it stopped and I have to repeat these commands
I tried to add it as a corn job to be run after machine reboot but it does not work
I post a question in the following link : run python app after server restart does not work using crontab
nothing work with me, and I have to make sure that this web app will always be running because it should be working to send push notification to mobile devices
can anyone please advice, I have been searching and trying for so many time
I'm not expert in it, but two solutions come in my mind:
1- Using systemd:
systemd can be responsible to keep services up.
You can write a custom unit for your app, and config it as a way to be up always.
This tutorial may be useful: writing unit
2- Using Docker:
When you have containerized app, you config it as to come up, on failure or anything like that.
Read about it here
What if you have the calling piece of Python script within a bash script and run that as a daemon:
Your bash script could like below (test.sh):
#!/bin/sh
cd desired/directory
python3 app.py
and you can run the bashscript like this by using nohup:
nohup ./test.sh 0<&- &>/dev/null &
You could refer this, if you want to store the outputs of nohup.

Why airflow scheduler and airflow webserver cant work together?

I run command airflow webserver in one terminal and it works well.
But when I run airflow scheduler in another terminal it stops webserver and can`t run scheduler too. I tried to change webserver port to 8070 but it still stucks.

How can I deploy a flask app on Lightsail with crash protection?

I am looking for help deploying my flash app. I've already written the app and it works well. I'm currently using the following command in the directory of my flask code:
sudo uwsgi --socket 0.0.0.0:70 --protocol=http -w AppName:app --buffer-size=32768
This is on my Amazon Lightsail instance. I have the instance linked to a static public IP, and if I navigate to the website, it works great. However, to get the command to continuously run in the background even after logging out of the Lightsail, I first start a screen command, execute the above line of code, and then detach the screen using ctrl-a-d.
The problem is, if the app crashes (which is understandable since it is very large and under development), or if the command is left running for too long, the process is killed, and it is no longer being served.
I am looking for a better method of deploying a flask app on Amazon Lightsail so that it will redeploy the app in the event of a crash without any interaction from myself.
Generally you would write your own unit file for systemd to keep your application running, auto restart when it crashes and start when you boot your instances.
There are many tutorials out there showing how to write such a unit file. Some examples:
Systemd: Service File Examples
Creating a Linux service with systemd
How to write startup script for Systemd?
You can use pm2
Starting an application with PM2 is straightforward. It will auto
discover the interpreter to run your application depending on the
script extension. This can be configurable via the Ecosystem config
file, as I will show you later on this article.
All you need to install pm2 and then
pm2 start appy.py
Great, this application will now run forever, meaning that if the process exit or throw an exception it will get automatically restarted. If you exit the console and connect again you will still be able to check the application state.
To list application managed by PM2 run:
pm2 ls
You can also check logs
pm2 logs
Keeping Processes Alive at Server Reboot
If you want to keep your application online across unexpected (or expected) server restart, you will want to setup init script to tell your system to boot PM2 and your applications.
It’s really simple with PM2, just run this command (without sudo):
pm2 startup
Pm2 Manage-Python-Processes

Run a Flask app forever, even after reboot

I have a running Flask app that is working fine on a Linux (CentOS) server.
To make it work in the background I started it within a screen session. There is scheduled downtime for the server every night. The next day all screen sessions are gone. How can I keep my Flask app or screen sessions always up and running after reboots?
Han Solo gave a good answer in his comment: if you run it as a systemd service, it'll run on boot, restart if it crashes, and so on.
However, if you just need a quick-and-dirty solution for a couple days while you configure the service properly, you can use cron:
Run crontab -e to edit your crontab file
Add a line at the bottom that looks like this:
#reboot [put your command here]
Save and exit the editor
sudo reboot to test it and make sure it works
And you're done! The command will now run whenever the system reboots.

Heroku Scheduler With Python Script

How do I run a python script that is in my main directory with Heroku scheduler?
Normally I run this through the command line with Heroku run python "script.py", but this syntax is clearly not correct for the Heroku Scheduler. Where it says "rake do_something" what should the correct syntax be to run a python script here? I've tried "python script.py" and this does not work either.
Thanks!
The Heroku Scheduler will try to run any command you give it. For Python, if you added a mytask.py to
your app repo, you could have the Scheduler run:
python mytask.py
Instead of waiting for the Scheduler to run to see if the command works as expected, you can also test run it like this:
heroku run python mytask.py # or heroku run:detached ...
heroku logs --tail
Another way to use the Scheduler would be to extend your app with a cli tool or a script runner that shares the app context. A popular one for Flask is Flask-Script.
Note: the "rake" references in the Heroku Scheduler docs example is for running tasks with ruby.

Categories