storing variable value

D

Daniel Dalton

Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before?
eg. can I write to a variable on one run of the program, and on the next
read that value that was written?

Thx!

Daniel.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAknggMsACgkQQCYsNYjKJVDhAQCguaGqsjCpcU62Zw5ZwUGfcxM4
bi4An3c7q0p5mLU/LM2lemBPZibmIoPb
=6LP+
-----END PGP SIGNATURE-----
 
D

Diez B. Roggisch

Daniel said:
Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before?
eg. can I write to a variable on one run of the program, and on the next
read that value that was written?

Use a file to store previous run's state. Use e.g. the pickle-module to
store some arbitrary data-structures to the disk.


Diez
 
S

Steven D'Aprano

Hi!

I'm writing a program to provide me with battery warnings when my
battery hits certain levels. It just checks the current level and does
something. I plan to call it from a a cron job. But If the cron runs
every minute, warnings every minute would be rather annoying. so is
there a way to make the script check if it has already ran before? eg.
can I write to a variable on one run of the program, and on the next
read that value that was written?

No, every time cron calls your script, it calls it as new. If you want
persistent storage, you need to write to persistent storage like a file.

Another solution is to use a daemon which is running continuously in the
background. Because that's a long-running process, the daemon can then
decide how often it checks the battery, and how often it notifies you.
However, writing correct daemons is not something for a beginner. I
recommend you just stick to writing your persistent data to a file
somewhere, and sticking to cron.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,501
Members
48,189
Latest member
StaciLgf76

Latest Threads

Top