Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:49:07+00:00 2026-05-13T05:49:07+00:00

I have a bash script that I am run to check to see if

  • 0

I have a bash script that I am run to check to see if one of my programs has hung, and if it has kill it. The script works fine if ran from the command line, but if I schedule it with cron it does something very strange.

Basically the script (below) gets the PID of my program and gets its created date/time from its entry in the /proc/ directory. It then gets the current date/time from the system and converts these two values into seconds since 1970 with the “date” command, before finally subtracting the two. This usually ends up with a total of 2100 seconds or something like that, which equates to 35 minutes.

#!/bin/bash
THEDATE=$(date +%s)
MYPID=$(ps aux|grep -v grep|egrep "MyProgram.exe"|awk '{print $2}')
if (( ${#MYPID} > 0 )); then
    STARTTIME=$(ls -ld /proc/$MYPID|date +%s -d"$(awk '{print $6, $7}')")
    TOTALMINS=$(( ($THEDATE - $STARTTIME) / 60  ))
    if (( $TOTALMINS >= 30 )); then
        kill -9 $MYPID
        logger -t "[KillLongRunningProcesses] Killed my program which had been running for $TOTALMINS minutes"
    fi
fi

When ran from the command line, the two date variables (THEDATE and STARTTIME) both get the correct values. But when run by cron the STARTTIME is wrong. It has the correct date, but seems to ignore the time part and sets it to midnight, ie “2009-12-14 00:00:00” is obtained instead of “2009-12-14 13:23:00”, which throws off all the calculations.

Any ideas? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:49:07+00:00Added an answer on May 13, 2026 at 5:49 am

    First off never parse the output of ls, read THIS to understand why. Next, your script can be greatly improved by using pgrep rather than using awk to parse the PID from a grep on ‘ps aux’. Also, your script breaks horribly in the case where you have more than one PID returned. And finally, when writing shell scripts try not to use CAPITALS for variable names; that convention is reserved for variables that you export into your environment.

    The following script attempts to solve the problems mentioned above. It is as efficient as I could make it and it will handle the case where you have multiple PIDs. It also checks to make sure that the PID still exists before we kill it because it’s possible that when we kill the parent it may take out the child processes.

    #!/bin/bash
    
    prog_name="MyProgram.exe"
    the_date=$(date +%s)
    my_pids=( $(pgrep "$prog_name") )
    
    for ((i=0; i < ${#my_pids[@]}; i++)); do
        if [[ -d /proc/${my_pids[i]} ]]; then
            start_time=$(stat --printf=%Y /proc/${my_pids[i]})
            total_mins=$(( (the_date - start_time) / 60 ))
            if (( $total_mins >= 30 )); then
                kill -9 ${my_pids[i]}
                logger -t "Your custom message here"
            fi
        fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this bash script on the server that runs every hour, via cron.
I have a bash script that cuts out a section of a logfile between
I have a bash script in which i check the exit code of a
I have a python script that'll be checking a queue and performing an action
Possible Duplicate: Quick-and-dirty way to ensure only one instance of a shell script is
Let me explain the tree structure: I have a network directory where several times
Wait is not waiting for all child processes to stop. This is my script:
I am trying to set up an automated build system on Windows using Cygwin.
I'm trying to get Ruby on Rails setup on my new eeeubuntu install and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.