schedule automate shell script running not as ROOT - python

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.

Related

Steam browser protocol failing silently when run over ssh

I am trying to launch a steam game on my computer through an ssh connection (into a Win10 machine). When run locally, the following python call works.
subprocess.run("start steam://rungameid/[gameid]", shell=True)
However, whenever I run this over an ssh connection—either in an interactive interpreter or by invoking a script on the target machine—my steam client suddenly exits.
I haven't noticed anything in the steam logs except that Steam\logs\connection_log.txt contains logoff and a new session start each time. This is not the case when I run the command locally on my machine. Why is steam aware of the different sources of this command, and why is this causing the steam connection to drop? Can anyone suggest a workaround?
Thanks.
Steam is likely failing to launch the application because Windows services, including OpenSSH server, cannot access the desktop, and, hence, cannot launch GUI applications. Presumably, Steam does not expect to run an application in an environment in which it cannot interact with the desktop, and this is what eventually causes Steam to crash. (Admittedly, this is just a guess—it's hard to be sure exactly what is happening when the crash does not seem to appear in the logs or crash dumps.)
You can see a somewhat more detailed explanation of why starting GUI applications over SSH fails when the server is run as a Windows service in this answer by domih to this question about running GUI applications over SSH on Windows.
domih also suggests some workarounds. If it is an option for you, the simplest one is probably to download and run OpenSSH server manually instead of running the server as a service. You can find the latest release of Win32-OpenSSH/Windows for OpenSSH here.
The other workaround that still seems to work is to use schtasks. The idea is to create a scheduled task that runs your command—the Task Scheduler can access the desktop. Unfortunately, this is only an acceptable solution if you don't mind waiting until the next minute at least; schtasks can only schedule tasks to occur exactly on the minute. Moreover, to be safe to run at any time, code should probably schedule the task for at least one minute into the future, meaning that wait times could be anywhere between 1–2 minutes.
There are also other drawbacks to this approach. For example, it's probably harder to monitor the running process this way. However, it might be an acceptable solution in some circumstances, so I've written some Python code that can be used to run a program with schtasks, along with an example. The code depends on the the shortuuid package; you will need to install it before trying the example.
import subprocess
import tempfile
import shortuuid
import datetime
def run_with_schtasks_soon(s, delay=2):
"""
Run a program with schtasks with a delay of no more than
delay minutes and no less than delay - 1 minutes.
"""
# delay needs to be no less than 2 since, at best, we
# could be calling subprocess at the end of the minute.
assert delay >= 2
task_name = shortuuid.uuid()
temp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".bat", delete=False)
temp_file.write('{}\nschtasks /delete /tn {} /f\ndel "{}"'.format(s, task_name, temp_file.name))
temp_file.close()
run_time = datetime.datetime.now() + datetime.timedelta(minutes=delay)
time_string = run_time.strftime("%H:%M")
# This is locale-specific. You will need to change this to
# match your locale. (locale.setlocale and the "%x" format
# does not seem to work here)
date_string = run_time.strftime("%m/%d/%Y")
return subprocess.run("schtasks /create /tn {} /tr {} /sc once /st {} /sd {}".format(task_name,
temp_file.name,
time_string,
date_string),
shell=True)
if __name__ == "__main__":
# Runs The Witness (if you have it)
run_with_schtasks_soon("start steam://rungameid/210970")

Python script and Google Cloud Engine [duplicate]

I have two Python scripts on my machine that I want to execute two times a day on specific time period. How do I automate this task? Since I will be away from home and thus my computer for a while, I want to upload them to a site and be executed from there automatic without me doing anything.
How can I do this?
You can use cron for this if you are on a Linux machine. Cron is a system daemon used to execute specific tasks at specific times.
cron works on the principle of crontab, a text file with a list of commands to be run at specified times. It follows a specific format, which can is explained in detail in man 5 crontab
Format for crontab
Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously. Code:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.
The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter which somecommand in the terminal to find the full path to somecommand. The crontab will begin running as soon as it is properly edited and saved.
You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)
*/10 * * * * /usr/bin/somedirectory/somecommand
which is also equivalent to the more cumbersome
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand
In Windows I have come up with two solutions.
First option: Create a .bat file.
Step 1
Create a .bat file to indicate the command you want to run and the script file that will be executed, for instance:
start C:\Users\userX\Python.exe C:\Users\userX\PycharmProjects\Automation_tasks\create_workbook.py
Step 2
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
Step 3
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not, check the option Run with highest privileges and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2.
Step 4
In the Actions tab, click on the New button and type in the following:
In Program/Scripts you need to look up for the Powershell path that the Task Scheduler will invoke to run the .bat file. In my case, my Powershell path was:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
In Add arguments (optional) you need to type the path of the file that will be executed by Powershell. In my case, the path was:
C:\Users\userX\Desktop\run_the_bat_file.bat
In Start in (optional) you need to type the path of the file but without the name of the .bat file, that is:
C:\Users\userX\Desktop\
Step 5
Click on the Triggers tab and select how often you want to execute this task.
Step 6
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
Second option: Run the .py file with the Task Scheduler
Step 1
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
Step 2
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not, check the option Run with highest privileges and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2.
Step 3
In the Actions tab, click on the New button and type in the following:
In Program/Scripts you need to look up for the Python.exe path that the Task Scheduler will invoke to run the python script. In my case, my Python.exe path was:
C:\Users\userX\python.exe
In Add arguments (optional) you need to only type the name of your python script. In my case, the path was:
Permissions_dump.py
In Start in (optional) you need to type the path of the file but without the name of the python script, that is:
C:\Users\userX\PycharmProjects\1099_vendors_costs
Step 4
Click on the Triggers tab and select how often you want to execute this task.
Step 5
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
Another option (in case you convert a .py to a .exe)
If you use the library Cx_Freeze to convert a .py to a .exe and you want to use the task scheduler to automate this task then you need to follow these steps:
Step 1
Click on Create Task and then click on the Actions tab to type in the following:
In Program/Scripts you need to look up for the C:\Windows\explorer.exe path that the Task Scheduler will invoke to run the .exe script.
In Add arguments (optional) you need to only type the name of your .exe file: CustomerPopulation.exe
In Start in (optional) you need to type the path of the file but without the name of the .exe file, that is:
C:\Users\userX\PycharmProjects\executables
In the General tab, make sure to have selected the Run only when user is logged on and have unchecked the Run with the highest privileges.
If reports stopped working
Make sure to check if your password hasn’t expired, otherwise the reports won’t be sent.
References:
https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
https://www.youtube.com/watch?v=oJ4nktysxnE
https://www.youtube.com/watch?v=n2Cr_YRQk7o
If you are using OSX then launchd is the preferred way to schedule tasks. There is a OSX CLI for launchd called launchctl but if you prefer a GUI my preferred one is launchcontrol.

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

Ubuntu Cron Job Screen

I am trying to run a program that runs this line of code in
screen livestream_dl -u "..." "..."
And it then starts a process of checking if an Instagram Live video is being active then Downloads it if it can find it and if not the screen terminates.
My issue is I have placed this into the cron job by adding this
*/1 * * * * screen livestream_dl "name" "igname" -p "pass"
Firstly, should I leave it as screen or without screen which would work to get the result if I were to do the same through SSH command line
Secondly, how can I improve this cron job to make sure if the Cronjob is already running then to ignore the command and check again in a minute?
Thanks very much! :)
You should definitely remove screen. screen is used to make terminal session resumable from another TTY. Since a cron job is not interactive by definition you won't need it.
In order to prevent cron from starting two instances of the job at the same time you can use flock. flock will create a semaphore file on the first run. If such a file is found it will either wait for the semaphore to be removed or terminate immediately if you specify the nonblack option. The later is probably what you want.

Running a Python Script every n Days in Django App

I am looking to run a Python script every n days as part of a Django app.
I need this script to add new rows to the database for each attribute that a user has.
For example a User has many Sites each that have multiple Metric data points. I need to add a new Metric data point for each Site every n days by running a .py script.
I have already written the script itself, but it just works locally.
Is it the right approach to:
1) Get something like Celery or just a simple cron task running to run the python script every n days.
2) Have the python script run through all the Metrics for each Site and add a new data point by executing a SQL command from the python script itself?
You can use Django's manage commad, then use crontab to run this command circularly.
The first step is to write the script - which you have already done. This script should run the task which is to be repeated.
The second step is to schedule the execution.
The de-facto way of doing that is to write a cron entry for the script. This will let the system's cron daemon trigger the script at the interval you select.
To do so, create an entry in the crontab file for the user that owns the script; the command to do so is crontab -e, which will load a plain text file in your preferred editor.
Next, you need to add an entry to this file. The format is:
minutes hours day-of-month month day-of-week script-to-execute
So if you want to run your script every 5 days:
0 0 */5 * * /bin/bash /home/user/path/to/python.py

Categories