Run my python3 program on remote Ubuntu server every 30 min - python

I have correct python3 program looking like *.py.
I have Digital Ocean(DO) droplet with Ubuntu 14.04.
My program post message to my twitter account.
I just copy my *.py in some directory on DO droplet and run it with ssh and all works fine.
But I need to post message(rum my program) automatically every 15-30 min for example.
Iam newbie with this all.
What should i do? Step-by-step please!

cron is probably the way to go - it's built for this task. See this DigitalOcean tutorial for details.
This StackOverflow answer explicitly states what to put in for a 30 minute repeat interval.
If you don't want to use cron for some reason, you could do something like:
import time
# Repeat forever
while True:
post_to_twitter() # Call your function
# Sleep for 60 seconds / minute * 30 minutes
time.sleep(60 * 30)

First install and enable fcron. Then, sudo -s into root and run fcrontab -e. In the editor, enter */30 * * * /path/to/script.py and save the file. Change 30 to 15 if every 15 minutes is what you're after.

Related

How to run selenium script with fixed time and how to host it

I created a script for automating a system. But I am stuck with the hosting the script and with the time. Lets discuss about the time first.
Here is my code:
from ray import SMSBot
import schedule
with SMSBot() as sms:
def execute():
sms.go_to_dashboard("https://sms.rayadvertising.com/login")
sms.login_to_dashboard("*****", "*****")
sms.find_number_section()
schedule.every(1).minute.do(execute())
schedule.every(20).minute.do(execute())
schedule.every(2).hour.do(execute())
schedule.every(12).hour.do(execute())
schedule.every(24).hour.do(execute())
schedule.every(26).hour.do(execute())
I tried this code. But I am afraid of taking this script on deployment.
What I want:
I want the script run after 5 min then 20 min then 2 hour then 12 hour then 24 hour then 26 hour.
how can I do that? Is my code ok?
second thing is I want to host it. Where and how can I host this script. I am running ubuntu in laptop I know I can run the script in my laptop but its not possible to keep running the laptop whole day.
Is there any help for me?

Anacron will not run notifications from python/bash scripts

The basics
I am trying to add desktop notifications to some fairly simple scripts running with anacron just to let me know when they are running and when they have finished. For some reason, the scripts DO run, but the notifications never get sent. If I run the scripts manually (that is to say, using ./bash_test.sh instead of sudo anacron -fdn testing), the notifications send just fine.
The scripts
The bash script I am trying to run looks like this:
#!/bin/bash
python -c 'import notifications; notifications.clamstart()'
#some clamav scanning stuff happens in here, this bit runs fine
python -c 'import notifications; notifications.clamfinish()'
and the corresponding notifications.py file looks like:
from plyer import notification
def clamstart():
notification.notify(
message="Security script has started running.",
app_name="Clam Scan Daily",
hints={"desktop-entry":"clamtk"}
)
def clamfinish():
notification.notify(
message="Security script has finished running.",
app_name="Clam Scan Daily",
hints={"desktop-entry":"clamtk"}
)
Supplemental info
These two files are in the same directory, so as far as I'm aware the import statements should work fine (and they do, when I run it with ./bash_test.sh)
I have already tried using notify-send, that was what I had set up initially and it was running into the same problem, which is why I decided to try and switch to python's plyer notify() and see if that worked.
ALL of these components work fine individually, they only stop working when I try to run them using anacron with sudo anacron -fdn testing
I believe the anacrontab is set up properly since it runs except for the notifications bit, but just in case I'll add it here too:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
#monthly 45 cron.monthly nice run-parts /etc/cron.monthly
1 10 backup-script /home/glottophilos/backup_daily.sh
7 15 virus-scan /home/glottophilos/clamscan_daily.sh
1 10 testing /home/glottophilos/testscript.sh
I should also note that I am pretty opposed to the idea of using cron instead of anacron because this is a setup for a personal rig that is not on all the time. If there is another way of handling the scheduling though that doesn't require anacron at all, I'm happy to explore that option!
This is NOT a duplicate of Using notify-send with cron They are similar, but the answer that I posted has some structural differences and works where the other does not for reasons I'm not entirely sure of.
Solution
OK, so the solution, as per #Nick ODell's direction in the comments on the original, appears to have been doing this in the etc/anacrontab file:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
#monthly 45 cron.monthly nice run-parts /etc/cron.monthly
1 10 backup-script sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/backup.sh>
7 15 virus-scan sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/clamscan-daily.sh>
1 10 testing sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/testscript.sh>
and then using this format in the bash script (avoiding python altogether):
notify-send --app-name="Clam Scan Daily" --hint=string:desktop-entry:clamtk "Security script is running."

How to schedule a periodic task in Python?

How can I schedule a periodic task in python without blocking?
Here is a simple situation. Assume this ticket variable becomes invalid after 2 hours. So I need to fetch it from a serve.
ticket = 1 # It expires every 2 hours
def process_using_ticket(): # This function is called using a get request from flask server
print('hello', ticket)
How can I reset to 1 every two hours without blocking?
One way could be to start a thread and sleep for 2 hours and then reset the variable but I am wondering if there are better alternatives.
Note: Everything runs in a docker.
This looks like a use case for cron, a simple utility found on all linux machines to schedule periodic tasks. A simple cron task could be created like:
$ crontab -e
Within the cron, make an entry
0 */2 * * * /home/username/ticket_script.py
Make sure that your script has executable permissions. In case you are printing something in your script, make the cron entry to redirect its ouput like
0 */2 * * * /home/username/ticket_script.py >> /home/username/ticket_script_runs.log
For gotchas on running this in Docker, you can go through this thread -> https://forums.docker.com/t/cronjobs-in-docker-container/2618/5 which says:
Most Docker containers only run the binary you specify (in this case /bin/bash), so there is no cron daemon running to trigger these jobs.
There are a number of ways you can deal with this - for small adhoc things, you can add the cron entry to your host's crontab, and trigger the job in the container using docker exec containername ...
You can replace the container entrypoint with a shell script that starts the cron, or if you have a need for several services, use something like supervisord to manage them.
And if you're going all in, you can make a cron container which you can then use to kick off tasks using docker exec.
The other approach is what you already have - go to sleep for 2 hours, or maintain a time_last_read timestamp, keep evaluating if its been 2 hours since your last read (time_now - time_last_read >= 2_HOURS), and re-read the value into memory once the condition is True and reset time_last_read.
Put it in a function with time.sleep(7200000). Like so:
import time
ticket = 1
def main_loop():
while True:
time.sleep(7200000)
ticket = 1
def process_using_ticket():
print('hello', ticket)
main_loop()

schedule automate shell script running not as ROOT

I have a shell script that I want to run automatically every day at 08 AM, and I am not authorised to use the crontab because I don't have root permission
My home directory is /home/user1/.
Any suggestions?
Ideally you should have your system administrator add your user account to /etc/cron.allow - without that you do not have permission to use the crontab command, as you probably discovered.
If that is not possible, then you could use a wrapper shell script along these lines (note: untested):
#!/bin/bash
TIME="tomorrow 8am"
while :; do
# Get the current time in seconds since the Epoch
CURRENT=$(date +%s)
# Get the next start time in seconds
NEXT=$(date +%s -d "$TIME")
# Sleep for the intervening time
sleep $((NEXT-CURRENT))
# Execute the command to repeat
/home/user1/mycommand.sh
done
You start the wrapper script in the background, or e.g. in a screen session, and as long as it's active it will regularly execute your script. Keep in mind that:
Much like cron there is no real accuracy w.r.t. the start time. If you care about timing to the second, then this is not the way to go.
If the script is interrupted for whatever reason, such as a server reboot, you will have to somehow restart. Normally I'd suggest an #reboot entry in crontab, but that seems to not be an option for you.
If there is some sort of process-cleaning mechanism that kills long-term user processed you are probably out of luck.
Your system administrator may have simply neglected to allow users access to cron - or it may have been an explicit decision. In the second case they might not take too well to you leaving a couple of processes overnight in order to bypass that restriction.
Even if you dont have root permission you can set cron job. Chcek these 2 commands as user1, if you can modify it or its throwing any error.
crontab -l
If you can see then try this as well:
crontab -e
If you can open and edit, then you can run that script with cron.
by adding this line:
* 08 * * * /path/to/your/script
I don't think root permission is required to create a cron job. Editing a cronjob that's not owned by you - there's where you'd need root.
In a pinch, you can use at(1). Make sure the program you run reschedules the at job. Warning: this goes to heck if the machine is down for any length of time.

Python crontab stops without error

I have a Python/Django application running on an Ubuntu Server. The application updates the stock for a webshop.
I have made a job in crontab to update the stock every night.
# daily stock update, starts at 23:30
30 23 * * * ./Cronjobs/stockUpdate.sh
stockUpdate.sh:
#!/bin/bash
cd Projecten
source booksVenv/bin/activate
cd Books/books
cat stockUpdate.py | python manage.py shell
stockUpdate.py:
from core.models import Stock
Stock().getAllStock()
Running Stock().getAllStock() by hand works fine.
Example: I login on the server via ssh, start the virtual environment, start django shell and run the getAllStock method.
However, contrab just seems to stop while running getAllStock without an error. A log is placed in /var/mail/.
When I open the file with nano here is what I get.
# more than 500 pages of prints
Go to next page.
>>>
Here is what I think could be going wrong:
* I use too many print statements in my code, they mess up the job.
* The job starts at 23:30 but takes a few hours, it is stopped the next day (after a half hour).
Can someone please tell me why this is occurring and give me some details as to how to debug and fix the issue.

Categories