Run python script in future

I have a python script that needs to be run on user inputs. I get the date and time from the user, and the script will be run on that date.

import time from datetime import datetime today = datetime.today() enter_date = raw_input("Please enter a date:(2017-11-28): ") enter_time = raw_input("What time do you want to start the script? ") difference = enter_date - today time.sleep(difference) 

After that, the main () function will work.

I could not get it to work. Is there a better way to achieve this? Thanks

+5
source share
3 answers
 import time from datetime import datetime date_format = "%d/%m/%y %H:%M" today = datetime.today() print("Please enter date in format dd/mm/yy HH:MM") enter_date = datetime.strptime( raw_input("> "), date_format) difference = enter_date - today print(difference) #time.sleep(difference.total_seconds()) 

Uncomment the dream if you want. However, this is a bad decision. For this you need an OS. For example, you can add a line with you script to a cronjob file on linux, etc. https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

+2
source

As you can complete cron jobs, a good solution would be to split your script into two parts, one to request when the script will be executed, and the other the actual script.

Your first script will create a cron job to schedule script execution, for which you can use the python-crontab package:

 pip install python-crontab 

To create a cron job:

 from datetime import datetime from crontab import CronTab run_date_input = raw_input("Please enter a date (eg 2017-11-28): ") run_time_input = raw_input("What time do you want to start the script (eg 14:30:12)? ") run_date = datetime.strptime(run_date_input, "%y-%m-%d") run_time = datetime.strptime(run_time_input, "%H:%M:%S") #Access the crontab for the current user (on unix systems) user_cron = CronTab(user='your username') #Access the crontab (on windows sytems) user_cron = CronTab(tabfile='c:/path_to_your_tab_file/filename.tab') #Create a new job job = user_cron.new(command='python /home/your_second_script.py') job.setall(datetime(run_date.year, run_date.month, run_date.day, run_time.hour, run_time.minute, run_time.second)) 

If you use windows, you need to install cron for Windows using something like cygwin or the new Linux subsystem for Windows 10.

+1
source

Here you go

 import time from datetime import datetime today = datetime.today() input_date = raw_input("Please enter a date and time : 2017-12-31 18:02 : ") enter_date=datetime.strptime(input_date,'%Y-%m-%d %H:%M') difference = enter_date - today print difference.total_seconds() time.sleep(difference.total_seconds()) 

if you want to split the date and time use

 import time import datetime today = datetime.datetime.today() input_date = raw_input("Please enter a date 2017-12-31 : ") input_time=raw_input("What time do you want to start the script? 18:02 : ") enter_date=datetime.datetime.strptime(input_date,'%Y-%m-%d') enter_time=input_time.split(':') date_with_time = enter_date + datetime.timedelta(hours=int(enter_time[0]),minutes=int(enter_time[1])) difference = date_with_time - today print difference.total_seconds() time.sleep(difference.total_seconds()) 

But the preferred way would be to use cron jobs to find more about cron here

0
source

Source: https://habr.com/ru/post/1273713/


All Articles