0

I have a .c file and this file has to be executed only at a particular time interval say 9pm. So, please let me know if there is any possibility of achieving this via python scripting.

Manje
  • 411
  • 4
  • 7
  • Please see my updated answer that covers the scheduling part of the question. – TomServo Jul 03 '17 at 10:24
  • Updated answer again to show how to schedule using either cron or python. Hope this will do it for you! Please "Accept" if it solves your problem completely, thanks. – TomServo Jul 03 '17 at 10:32
  • 2
    Possible duplicate of [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) – Alessandro Da Rugna Jul 03 '17 at 12:40

2 Answers2

1

Check out the subprocess module in the standard python library. Something like:

from subprocess import call

Then when you want to issue your command:

call(["yourprogramname", "argument1"])

Of course, you need to get your C program into an executable form and make it executable on your platform. This will allow you to decide when to run the program under control of python (hour 21=9pm):

from datetime import datetime
from threading import Timer

x=datetime.today()
y=x.replace(day=x.day+1, hour=21, minute=0, second=0, microsecond=0)
delta_t=y-x

secs=delta_t.seconds+1

def runmycommand():
    <your call command here>
    #...

t = Timer(secs, runmycommand)
t.start()

However, if you're on a on a unix/linux platform, you can make it run on a schedule by adding it to crontab:

0,14,29,44 * * * * /yourprogramlocation/yourprogramname

To learn how to schedule with cron use the man cron command or see a tutorial like this one.

TomServo
  • 7,248
  • 5
  • 30
  • 47
0

What previous answers miss is your question about how to trigger at a specific time, e.g. 9pm.

You can use cron for this. It's a *nix utility that executes arbitrary commands on a time schedule you specify.

You could set cron to run your C program directly, or to launch a Python wrapper that then starts your C executable.

To get started: type crontab -e and follow the examples.

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173