Reputation: 473
I was having a problem recently where somebody's cron job was calling a script that sent an alert to me when it was run. I wanted to find out whose job it was and which server it was running on.
The problem has been resolved by someone else, but I was wondering what I could have done to find out which host/username the job is being run from. One thing I could think of was to edit the script (Perl) and use Sys::Hostname. Anything else?
Thanks!
Upvotes: 0
Views: 566
Reputation: 247062
You could also add to your script: print `env|sort`;
-- that would reveal the USERNAME or LOGNAME. If you don't want to mess with the output of your program, log it to a file:
use POSIX 'strftime';
open my $log, '>>', 'logfile' or die "can't append to logfile: $!\n";
print $log strftime(%Y-%m-%d %T", localtime), " - starting $0\n";
print $log `env|sort`;
close $log;
Upvotes: 1
Reputation: 62109
As you said, you can get the hostname with Sys::Hostname. You can also get the username with getpwuid($<)
:
use Sys::Hostname;
my $info = getpwuid($<) . '@' . hostname;
print "$info\n"; # prints user@host
Upvotes: 2
Reputation: 328760
There is no automatic way to do that unless you use mail to send out the alerts. Mails contain the host name in the header, so you can at least see where it came from (user and host). The time stamp should then help to locate the cron job.
For all other forms of alerts (SMS, pager, etc), you should make it a policy to include the user and hostname in the message.
Upvotes: 1