Make a bash script that takes a program name as an argument and checks if there's a…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
Make a bash script that takes a program name as an argument and checks if there's a running process for it using pgrep
- Assistant
Here's a simple bash script that takes a program name as an argument and checks if there's a running process for it using
pgrep:bash#!/bin/bash # Check if an argument was provided if [ "$#" -ne 1 ]; then echo "Usage: $0 <program_name>" exit 1 fi # Use pgrep to find the process pgrep -x $1 > /dev/null # Check the exit status of pgrep if [ $? -eq 0 ]; then echo "Process $1 is running" else echo "Process $1 is not running" fiSave this script as
check_process.shand make it executable withchmod +x check_process.sh. You can then run it with a program name as an argument, like this:bash./check_process.sh program_nameThis script uses
pgrep -xto search for processes with the exact name provided as an argument. If a process is found,pgrepwill return 0, and the script will print "Process <program_name> is running". If no process is found,pgrepwill return a non-zero value, and the script will print "Process <program_name> is not running".
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.