2

I am running a script named statusCron.pl.

I want when I run that script, that script can detect/print its name which is statusCron.pl. By that I can detect its process ID so that if it found itself already running on the background it will kill itself.

By this I can eliminate hardcoding and re-apply it to all of my scripts.

Additional: I dont want the directory to be included.

1
  • Additional: I dont want the directory to be included. Commented Feb 25, 2015 at 5:58

4 Answers 4

2

You can get the name of running program in $0 variable and to avoid the directory name we use the module File::Basename to get only the basename.

    use File::Basename;
    my $name = basename($0);
    print $name;

man perlvar for other special variables

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the added information. It really helped.
2

You can use File::Basename like this:

use File::Basename;
print basename(__FILE__), "\n";

Comments

0

You can print both the name of the Perl script file and the process ID by writing this

printf("%s %d\n", $0 =~ m|([^\\/]+)$|, $$);

1 Comment

Thanks a lot, additional useful information. I might use this in the future.. :)
0
#!/usr/bin/perl
print $0;

$0 stores the name of the script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.