Using Perl to grab a proccess id in Linux

Ever since I first started doing Linux system administration, I’ve used perl scripts to automate some basic tasks. I’ve never really been that great at writing scripts because I never took the time to sit down and really learn the intricate details of the language. Here recently, I’ve written some of my most complex and detail oriented scripts, where I’ve had to really learn a little bit about regular expressions. Along the way I also learned how to pick very specific data out of a array and feed it to my script for processing.

Here is a simple script that utilizes the ‘ps’ command, grabs the process id (PID) from the array, and prints as an output.

#!/usr/bin/perl

@ps = `ps ax | grep sshd`;
foreach $proc (@ps) {
        @pid = split(/ +/,$proc);
        print "PID: $pid[0]\n";
}

The things that you can with this sort of script are endless in automating system administration.