Kill Python script using Cronjob (semi gracefully)


I am running Python on Ubuntu. I have been running a python script in a console session however I have now created it as a scheduled cron job.

I also want another job scheduled to kill the script however when I use
killl - 9 python3

I don't get the usual messages logged through Python telling me that the processes have exited

When running in a console session, I would normally kill the script using Ctrl-C. Is there a way in which to do this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Well you can write a python script that calls the kill command, using the os import.
But, before you even try, can you describe your scenario a little more, so we can help you with the most appropriate design for it?
aidebAuthor Commented:
Ah sorry, my command was wrong

I am running

kill -9 (pidof python3

I am not actually using python to kill it....I am using a shell command line
Kill sends a SIGTERM by default.  A CTRL-C is a SIGINT.  You can specify that's the signal that you want to send rather than a SIGTERM.

kill -SIGINT <pid of python process>

The above should send a CTRL-C to your python process.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aidebAuthor Commented:
Fantastic. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.