Bash – CRON job that kills a process started by previous CRON

bashcron

I need to create a cron job that runs a bash script in background which does not end unless killed.
The bash starts a process that should keep running for about 28 hours then I need another cron job to kill it.
First cron runs every day at 0:00AM, starts the process.
Second cron runs at 4:00AM and has to kill the process started the day before, leaving the one for the current day to run.

From what I searched, I should store the pid of the process in a file and then have the second cron access it but how and where? In the cron or in bash? Considering the process started by bash script does not end until killed, will the commands after ever execute?

EDIT: Ipor Sircer's solution solves the particular problem I have but I'd still like to learn how to export the PID in a file which another CRON can access.

Best Answer

Use timeout command, it is much easier:

0 0 * * * timeout 28h script.sh
Related Question