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
lsofcommand to check if a port is in use - How to use the
pscommand 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.
Open a text editor and create a new file named
get.shin the/home/labex/projectdirectory.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 fiThis 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.
Add the following code to get the port number:
## Get the port number port=$1This code stores the provided port number in the
portvariable.Save the
get.shfile.
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.
Add the following code to the
get.shscript:## Check if the port is in use process=$(lsof -i :$port -sTCP:LISTEN -Fp | sed 's/^p//')This code uses the
lsofcommand to check if a process is listening on the specified port. The output is stored in theprocessvariable.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}' fiThis code checks if the
processvariable 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 thepscommand to get the full path of the program and prints it.Save the
get.shfile.
Test the get.sh Script
In this step, you will test the get.sh script to ensure it works as expected.
Open a terminal and navigate to the
/home/labex/projectdirectory.Run the
get.shscript with a port number as an argument:$ sh get.sh 3000 /usr/lib/code-server/lib/nodeThis should output the full path of the program running on port 3000.
Run the
get.shscript with a port number that is not in use:$ sh get.sh 43000 OKThis should output "OK" since no program is running on port 43000.
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.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



