From your Perl code, you can manage programs running on your machine, including the Perl program itself. In this article, we will learn how to control processes from Perl. Have a nice reading.
Executing System Commands
There are several ways to execute a system command from Perl code. Let’s see them together.
The first method to execute a system command is using the backticks. If you are a UNIX/Linux user, you should have used backticks to assign a command output to a shell variable. In a similar way, a system command enclosed within a pair of backticks can be executed, and has its value assigned to a scalar or array variable. The next two examples illustrate the use of backticks to run system commands under UNIX/Linux and Windows.
Obtaining Julian Date Example
The Julian date (or day) represents the day of year. It ranges between 001 and 365 (or 366 in the leap year). To obtain the Julian day of today, UNIX/Linux systems use the following command:
As of the date of writing these lines (February, 1st 2016), the above command should give the following output:
Consider you need to write some data to a file on a daily basis. You need to create a new separate file every day, with a constant name part, and a varying suffix which is the Julian day. To do this, we need to extract the Julian day from the system. Check the following script:
$JULIAN = `date +%j`;
open (OUT, " > dailyfile.$JULIAN");
print OUT ("Data written to the dailyfile\n");
When executed, the script should create a file named dailyfile with the Julian day as extension.
Okay, this is in UNIX/Linux. What about Windows machines?
Obtaining IP Address of a Windows Machine
To obtain the IP address of a windows machine, type ipconfig in command prompt:
How to extract the IP address from this output? Let’s see.
@output = `ipconfig`;
/IP/ and print;
Executing this script from the command line should give output similar to the following:
While the command output was stored in a scalar variable $JULIAN (in the Julian day example), the ipconfig command output has been assigned to an array @output, one line in each element.
The foreach loop iterates over the array elements (output lines), and prints any line containing the word IP.
The system Function
The system function is used to run a program or operating system command. The function accepts an array (list) that represents the command to execute, the options, and command arguments.
system(COMMAND, OPTION, ARGUMENT1, ARGUMENT2,..,ARGUMENTn);
Inside a Perl script in your Linux box, you need to get the hostname of another UNIX/Linux machine. We need to run the hostname command on the remote machine. To run a command on a remote machine, the ssh utility can be used. To automate the process, and not get a prompt for password, the sshpass command is used to provide the connection password in the command. The full command should be:
sshpass -p "redhat" ssh firstname.lastname@example.org 'hostname'
How could we run this from a Perl script? The following code will give you the answer:
@command = ("sshpass", "-p", "redhat", "ssh", 'email@example.com', "hostname");
When executed, the above script should print the hostname of the remote machine.
The idea is simple: a list is formed containing the command to run (sshpass), an option (-p), argument to option (redhat), and the arguments: ssh, username@IPaddress, and the command to execute remotely (hostname). The resulting list is passed as an argument to the system function, which executes the command with its options and arguments.
On a windows machine, we want to open the calculator from Perl code.
This will start the Microsoft Calculator.
Using the exec Function
The exec function also can be used to execute a program or system command from Perl scripts. Its syntax is identical to that of the system function.
Terminating Running Processes
As we can execute commands and start programs from Perl scripts, we can also terminate them on need. If you are a UNIX/Linux guy, you certainly know the kill command. Perl has a built-in function having the same name and job as well, which is killing a process.
From your Perl script, you need to identify and kill httpd processes. How will you do it?
@PID_list = ();
@processes = `ps -ef`;
foreach $line (@processes)
if($line =~ /httpd/)
@list = split(/\s+/,$line);
kill(9,@PID_list) if(scalar(@PID_list) > 0);
print (scalar(@PID_list), " processes killed\n");
Let’s see how this will execute:
When executed, the script should kill the above 9 processes.
Now, the output of ps –ef command should have no httpd processes:
Terminating the Current Program
If the logic of the program needs this, the current program could be terminated using either die or exit functions.
The die Function
The die function terminates the current program, and prints a message to the standard error. A common use for the die function is combined with the open function.
open(INFILE,"< /opt/data01.txt") or die ("File doesn't exist\n");
If the file to be opened doesn’t exist, the program is terminated and the above error message is written to the standard error.
The exit Function
When called, the exit function terminates the current program with optionally-specified exit code (status).
For example, the following line will try to open a file. If the file doesn’t exist (or can’t be opened), the program exits with exit code 99.
open(INFILE,"< /opt/data01.txt") or exit(99);
This should give us the following output:
- There are several ways to execute system commands, or to run programs from Perl code.
- The backticks
- The system and exec functions are used to run a program, or execute a system command from Perl scripts.
- The kill function is used to terminate one or more processes.
- The die function terminates the current program, and prints a message to the standard output.
- The exit function terminates the current program, and optionally returns an exit code.
In the next article, we will investigate the Perl IDE “Padre“. An article that could be a short break while climbing high mountains. See you there.