open-neuroscience / open-neuroscience.github.io

new implementation of Open Neuroscience website
https://open-neuroscience.com/
MIT License
16 stars 5 forks source link

Create Cron job to run the Autopost.R function #23

Open amchagas opened 3 years ago

amchagas commented 3 years ago

This is mostly done on my side. I have no idea how to fix what we discussed today, so I asked on StackOverflow https://stackoverflow.com/questions/61960739/prevent-writelines-overwriting-md-files

remainder is to write the little cron job to run autoposter.R every once in a while

Originally posted by @matiasandina in https://github.com/open-neuroscience/open-neuroscience-website/issues/11#issuecomment-632826369

matiasandina commented 3 years ago

How are we currently running autoposter.R? From a Raspberry?

This is what I use for my raspberries. Things to check, "Rscript ~/path_to_file/autoposter.R" might not work as I expect it to, also add the correct path. We also don't want this to run once a minute so maybe change job.minute.every(1) to job.hour.every(6) ? or something else you like

from crontab import CronTab
# this is what we want to run
cmd_command = "Rscript ~/path_to_file/autoposter.R"

job_comment = "send ip to choilab1"

# Get cron object
cron = CronTab(user="pi")
# don't create a new job if it is already there
# first check if it's already there

job_exists = any(list(map(lambda x: x.comment == job_comment, cron)))

if job_exists:
    for job in cron:
        if job.comment == job_comment:
            print("job named: " + job_comment + " already exists, scheduling 1 minute")
            job.minute.every(1)
            # write the program
            cron.write()
else:
    # only here, create it
    # create a new job
    job = cron.new(command = cmd_command, comment=job_comment)
    # schedule it every minute
    job.minute.every(1)
    # write the program
    cron.write()

print("Current cron tab (same as $ crontab -l)")
print("--------------------------------------")
print(cron)