#!/usr/bin/env perl use warnings; use strict; use IPC::System::Simple 'capturex'; my @cmd = ('pgrep','-f','bash'); my $msg = "Alive"; my $outfile = 'file.txt'; my $count = 5; my $sleeptime = 1; open my $fh, '>>', $outfile or die "Could not open $outfile! $!"; for (1..$count) { my $timestamp = localtime(time); my @pids = capturex(@cmd); chomp(@pids); if (@pids) { print $fh join(',',@pids) . " , $timestamp , $msg\n"; } else { print $fh "No pid , $timestamp\n"; } sleep $sleeptime; } print $fh "Time over , ".localtime(time)."\n"; close $fh;