Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

How to know if a process is launched from cron in python

We have a use case where we need to distinguish if a process is launched from crontab or if it is a background process in Python ?

Are there any process attributes which can be checked to know if it a cron launched process or it is a background process?

question from:https://stackoverflow.com/questions/65880097/how-to-know-if-a-process-is-launched-from-cron-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A process launched from cron will have cron as its parent process.

Of course, in the general case, a process launched from a process which was launched from cron will have cron as its grandparent; and if the launcher process was a child of cron but is now terminated, the child process will be reparented and have init (process number 1) as its parent. at which point you can no longer tell if it previously had cron as its parent.

None of this is specific to Python, but maybe check After starting process, how to get parent's PID in the child? for implementation details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...