Make a bash script that takes a program name as an argument and checks if there's a running process for it using pgrep
To create a bash script that takes a program name as an argument and checks if there's a running process for it using pgrep
, you can use the following code:
#!/bin/bash
# Check if the user provided a program name as an argument
if [ $# -eq 0 ]; then
# If not, print an error message and exit
echo "Error: Please provide a program name as an argument."
exit 1
fi
# Use pgrep to search for a running process with the given name
pgrep $1
# Check the exit code of the previous command
if [ $? -eq 0 ]; then
# If the exit code is 0, that means a process was found, so print a message
# indicating that the program is running
echo "The program '$1' is currently running."
else
# If the exit code is not 0, that means no process was found, so print a
# message indicating that the program is not running
echo "The program '$1' is not currently running."
fi
Save the code to a file named check_program.sh
, make the file executable by running chmod +x check_program.sh
, and then you can run the script by calling ./check_program.sh
followed by the name of the program you want to check. For example:
./check_program.sh bash
This will search for a running process with the name "bash" and print a message indicating whether or not it is running.
Thanks, can you also not show the process IDs if the program is running? And if the program is not running, exit with a non-zero status code
To modify the script to not show the process IDs when the program is running and to exit with a non-zero status code when the program is not running, you can use the following code:
#!/bin/bash
# Check if the user provided a program name as an argument
if [ $# -eq 0 ]; then
# If not, print an error message and exit
echo "Error: Please provide a program name as an argument."
exit 1
fi
# Use pgrep to search for a running process with the given name
pgrep $1 &> /dev/null
# Check the exit code of the previous command
if [ $? -eq 0 ]; then
# If the exit code is 0, that means a process was found, so print a message
# indicating that the program is running
echo "The program '$1' is currently running."
else
# If the exit code is not 0, that means no process was found, so print a
# message indicating that the program is not running and exit with a non-zero
# status code
echo "The program '$1' is not currently running."
exit 1
fi
This updated script will not show the process IDs when the program is running, and it will exit with a non-zero status code when the program is not running. Save the code to a file named check_program.sh
, make the file executable by running chmod +x check_program.sh
, and then you can run the script as before by calling ./check_program.sh
followed by the name of the program you want to check. For example:
./check_program.sh bash
This will search for a running process with the name "bash" and print a message indicating whether or not it is running. If the program is not running, the script will exit with a non-zero status code.