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 8197143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:40:55+00:00 2026-06-07T05:40:55+00:00

Here is my shell script: #!/bin/bash PIDS=$(ps -e | grep $1 |grep -v grep|

  • 0

Here is my shell script:

#!/bin/bash
PIDS=$(ps -e | grep $1 |grep -v grep| awk '{print $1}')
kill -s SIGINT $PIDS
echo "Done sendings signal"

I am passing the name of the process as command line argument.
The echo command is not getting executed, although the target processes are actually receiving the SIGINT signal and exited.

Any suggestions?

Update:
I changed the code to:

#!/bin/bash
PIDS=$(ps -e |grep $1 | grep -v grep | awk '{print $1}'|grep -v $$)
echo $PIDS
kill -s SIGINT $PIDS
echo "Done sendings signal"
echo "The current process is $$"

Now I am noticing a strange thing:
The script is working but not as expected. Executing following command in command line outside the script
ps -e|grep process-name|grep -v grep|awk '{print $1}'|grep -v $$
gives pid of the process-name but when I execute the same command inside shell script, assign it to variable PIDS and then echo PIDS then it shows one more pid in addition to the pid of process-name. Therefore when the kill command executes it gives an error that the process with second pid doesn’t exist. It does echo the remaining sentences in the terminal. Any clue ?

  • 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-06-07T05:40:56+00:00Added an answer on June 7, 2026 at 5:40 am

    Your script works. The only reason I can see for the echo not being executed is that some value of $1 and the script file name combine so that your script PID is also gathered, thereby making the script suicide.

    The PIDS line spawns a process running ps, grep, another grep — so you won’t find in PIDS the processes running grep, but what about the parent process itself?

    Try:

    #!/bin/bash
    PIDS=$(ps -e | grep $1 |grep -v grep | awk '{print $1}' | grep -v "^$$\$" )
    kill -s SIGINT $PIDS
    echo "Done sendings signal"
    

    or run the pipes one after the other with suitable safety greps.

    Edit: it is evident that the “$1” selection is selecting too much. So I’d rewrite the script like this:

    #!/bin/bash
    # Gather the output of "ps -e". This will also gather the PIDs of this
    # process and of ps process and its subshell.
    PSS=$( ps -e )
    # Extract PIDs, excluding this one PID and excluding a process called "ps".
    # Don't need to expunge 'grep' since no grep was running when getting PSS.
    PIDS=$( echo "$PSS" | grep -v "\<ps\>" | grep "$1" | awk '{print $1}' | grep -v "^$$\$" )
    if [ -n "$PIDS" ]; then
        kill -s SIGINT $PIDS
    else
        echo "No process found matching $1"
    fi
    echo "Done sending signal."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is the shell script: #!/bin/bash version=0.01; fibonacci() { n=${1:?If you want the nth
I have a shell script script here as below: #!/bin/bash CPUSELECTION=1 386SX off \
$0 expands to the name of the shell script. $ cat ./sample-script #!/bin/bash echo
I have a bash script which calls another bash script, like so: #!/bin/bash echo
My shell script is as shown below: #!/bin/bash # Make sure only root can
Here's my personal shell's 'head file' tool.h #!/bin/echo Warnning! this libiary must be scourced
Here's what I'm trying to achieve: open a shell (korn or bash, doesn't matter),
I want to do formatting using echo in shell scripting. Here is a small
In bash I can create a script with a here-doc like so as per
I have a script written the unix shell language (NOT in bash or any

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.