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

  • SEARCH
  • Home
  • 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 179105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:20:53+00:00 2026-05-11T14:20:53+00:00

We want to build a script that run every night (kills and restart a

  • 0

We want to build a script that run every night (kills and restart a java process). For that we need to capture the process number (since there could be more than one java process running). The command below is basically what we will use to obtain the processes number, probably with a regexp at the end of the grep. Unless any better suggestions comes up.

root#ps -e |grep  'java' 18179 pts/0    00:00:43 java 

We want to know how to parse the output above and get it into a shell variable so we can use the kill command as below.

kill -9 ${processid} wait 10 

Note1: The reason we cannot rely on the normal service stop command is because the processes sometimes does not want to die. And we have to use the kill command manually.

  • 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. 2026-05-11T14:20:53+00:00Added an answer on May 11, 2026 at 2:20 pm

    There are a couple of options to solve this. If you’re using bash, then the shell variable ‘$!’ will contain the PID of the last forked-off child process. So, after you start your Java program, do something like:

    echo $! > /var/run/my-process.pid 

    Then, after your init script stops the Java process:

    # Get the pidfile. pid=$(cat /var/run/my-process.pid)  # Wait ten seconds to stop our process. for count in $(1 2 3 4 5 6 7 8 9 10); do     sleep 1     cat '/proc/$pid/cmdline' 2>/dev/null | grep -q java     test $? -ne 0 && pid='' && break done  # If we haven't stopped, kill the process. if [ ! -z '$pid' ]; then     echo 'Not stopping; terminating with extreme prejudice.'     kill -9 $pid fi 

    Make sure to remove the pidfile when you’re done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to integrate an Ant script with the Java build process in Eclipse.
I want to run a post build script that installs the dll in the
I want to be able to launch a Run Script Build Phase in XCode
Not until midway through a 3 hour build script, I'll remember that I want
I guess I want to ultimately build out a script that will force the
I have a Silverlight solution that I want to create a build script for.
I have a build script calling xcodebuild. that works, but I want to also
I want to create an application that will automate a very specific build process
I want to create a post build script that moves files from the build
I'm trying to develop a bash build script for a Java project that will

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.