Learn about Managing Processes in Perl Programming

0
1610
Perl-(20-Managing-Processes-740X296

Perl-(20-Managing-Processes-740X296

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.

Using Backticks

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:

When executed, the script should create a file named dailyfile with the Julian day as extension.
1.1

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:
1

How to extract the IP address from this output? Let’s see.

Executing this script from the command line should give output similar to the following:
2

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.

Syntax

Example
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:

How could we run this from a Perl script? The following code will give you the answer:

When executed, the above script should print the hostname of the remote machine.
3.1

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.

Example
On a windows machine, we want to open the calculator from Perl code.

This will start the Microsoft Calculator.
3

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.

Learn the Basics of C Programming Language

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.

Syntax

Example
From your Perl script, you need to identify and kill httpd processes. How will you do it?

Let’s see how this will execute:
4

When executed, the script should kill the above 9 processes.
5

Now, the output of ps –ef command should have no httpd processes:
6

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.

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.

This should give us the following output:
7

Summary

  • There are several ways to execute system commands, or to run programs from Perl code.
  • The backticks can be used to run system commands.
  • 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here