-1

Say I want to delete the contents of a directory every hour, what is the difference between having let's say a python script do this for me: nohup python dir_cleaner.py & vs setting a cronjob to do this for me? Couldn't find a cut-clear explanation for this difference. Let's say I'd also like to have every hour the exact date + the files listed that were in that directory and have this output to a file prior to the files being deleted. What is the best approach? Apologies if duplicate.

1
  • @Quasímodo Okay I do see why it would be useful now to use cron. Thanks. – user21398 Jan 24 at 22:48
1

If you run a python script via nohup, it is run only once. So the script must itself work in an infinite loop and repeat deleting of the directory every hour.

Also, if your script fails for any reason and stops running, or it is killed, it won't restart by itself - you must restart it manually. You also won't know that your script isn't running anymore (other than occassionally checking the process list or the output file that you create).

If you setup a cron job to run every hour, then this job will be repeated automatically forever. Even if it fails one time, it will be run again the next hour.

Also, if you have mail set up on your system (you have some MTA installed and configured), then any output produced by a cron job (if any) will be mailed to you. So if your job normally does not produce any output, there will be no mail; but if it fails and there will be some error message, it will be mailed to you.

These are main differences to me. In my opinion a cron job is therefore more reliable than just letting a script run in background via nohup.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.