Get Program That Satisfies the Condition

ShellShellBeginner
Practice Now

Introduction

In this project, you will learn how to create a script that can check if a program is running on a specified port and print the full path of the program or "OK" if no program is running.

👀 Preview

## Example
$ cd /home/labex/project
$ sh get.sh 3000
/usr/lib/code-server/lib/node
$ sh get.sh 43000
OK

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a Zsh script
  • How to use the lsof command to check if a port is in use
  • How to use the ps command to get the full path of a running program

🏆 Achievements

After completing this project, you will be able to:

  • Write a script that can identify the program running on a specified port
  • Troubleshoot issues related to port conflicts in your development environment
  • Automate the process of checking for running programs on specific ports

Create the get.sh Script

In this step, you will create the get.sh script that will check if a program is running on a specified port.

  1. Open a text editor and create a new file named get.sh in the /home/labex/project directory.

  2. Add the following code to check if a port number is provided as an argument:

    ## Check if the port number is provided as an argument
    if [ -z "$1" ]; then
      echo "Please provide a port number."
      exit 1
    fi

    This code checks if the script was called with a port number as an argument. If not, it prints an error message and exits the script.

  3. Add the following code to get the port number:

    ## Get the port number
    port=$1

    This code stores the provided port number in the port variable.

  4. Save the get.sh file.

Check if a Program is Running on the Specified Port

In this step, you will add the code to check if a program is running on the specified port.

  1. Add the following code to the get.sh script:

    ## Check if the port is in use
    process=$(lsof -i :$port -sTCP:LISTEN -Fp | sed 's/^p//')

    This code uses the lsof command to check if a process is listening on the specified port. The output is stored in the process variable.

  2. Add the following code to check if a program is running:

    ## Check if a program is running
    if [ -z "$process" ]; then
      echo "OK"
    else
      ## Get the full path of the program
      path=$(ps -p $process -o args=)
      echo "$path" | awk '{print $1}'
    fi

    This code checks if the process variable is empty (i.e., no program is running on the specified port). If it is empty, the script prints "OK". If a program is running, the script uses the ps command to get the full path of the program and prints it.

  3. Save the get.sh file.

Test the get.sh Script

In this step, you will test the get.sh script to ensure it works as expected.

  1. Open a terminal and navigate to the /home/labex/project directory.

  2. Run the get.sh script with a port number as an argument:

    $ sh get.sh 3000
    /usr/lib/code-server/lib/node

    This should output the full path of the program running on port 3000.

  3. Run the get.sh script with a port number that is not in use:

    $ sh get.sh 43000
    OK

    This should output "OK" since no program is running on port 43000.

  4. If the script works as expected, you have completed the project.

Congratulations! You have created a script that can check if a program is running on a specified port and print the full path of the program or "OK" if no program is running.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Shell Tutorials you may like