0

Ok so I was trying to make a script that would only activate if it was a week after installing and first running it.

My first thought was to make a new environment variable with the activation time, and making the program check if the current time is larger than that, but I found that using os.environ['activation-Time'] = str(time.time() + 604800) does not actually add a new environment variable that can be accessed at a later time.

Is it possible to have a variable that gets saved each time you run the code? Or is it possible to create a new environment variable from within a python script?

Gurnoor
  • 133
  • 2
  • 7
  • 6
    Save it in a file – rdas May 31 '20 at 08:39
  • saving it in a file is the answer to accept really ^^ – lolu May 31 '20 at 08:49
  • Maybe you can save the result from Python script to environment variable? `MY_ENV_VARIABLE=$(python my_script.py)` – Andrej Kesely May 31 '20 at 09:52
  • FWIW, `activation-Time` seems to be an invalid env var name. Bash for example won't export it: ``bash: export: `activation-Time': not a valid identifier``. In any case, putting a hyphen in an env var name is probably bad practice, and so is camelCase. You could use something like `MY_SCRIPT_ACTIVATION_TIME` instead. – wjandrea May 31 '20 at 17:08

1 Answers1

2

I can think of a couple ways to do this, depending on your specific needs.

  1. You can use the python-crontab package to manage cron jobs on your system. I don't know if you are on Windows, Mac OS X, or linux, and don't know if this works on windows. But on a mac or linux, this allows you to set jobs to run on a schedule, and you could decide when you want this script to be run again.
  2. You could write the last (or first?) run-time to a sqlite database, and have a cron job check that at regular intervals, and decide when to run again based on whatever logic you want.
  3. I will say that environment variables are probably not the way to go, unless you write them into your shell .rc file, so that they are re-used by every shell you open. You still need something to try to run your script at a certain time, either manually or via a cron job.

Hope that helps, happy coding!

Sam
  • 1,406
  • 1
  • 8
  • 11