2

I have a custom version of CENTOS that I need to run a perl script as a daemon in at all times.

To do this I want to get it to run on startup as a daemon.

When the machine is on I can daemonize the script with the command

daemonize /var/myfolder/myscript.pl

And this works fine.

So I have an rc1 script which has a number of commands that run when the machine starts, and the very last line in it is where I try to daemonize that script. Everything else in the rc1 script runs fine, and the script doesn't output any errors, however when I check to see if the daemon is running on start up, it isn't running.

Is there another way that I can get the script to run on startup? Or any ideas on why this method doesn't work?

Spellchamp
  • 295
  • 4
  • 16

2 Answers2

2

Proc::Daemon offers what you're looking for.

See this previously asked question: How can I run a Perl script as a system daemon in linux?

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Unfortunately I can't use Proc::Daemon as I am unable to install it. (I'm missing yum, makeMaker.pm as well as a lot of other things) is there anything else I could try? – Spellchamp Aug 10 '15 at 12:15
  • Why don't you use CPAN? Also check this: http://stackoverflow.com/questions/14066907/installing-perl-dependency-automatically-in-perl/14067234#14067234 – Chankey Pathak Aug 10 '15 at 13:00
  • Just install App::cpanminus using "cpan App::cpanminus" command on your temrinal. Then type "cpanm Proc::Daemon". – Chankey Pathak Aug 10 '15 at 13:01
  • CPAN is also missing I'm afraid. I'll see if I can get it installed. – Spellchamp Aug 10 '15 at 13:59
  • 1
    Re "I can't use Proc::Daemon as I am unable to install it.", Sure you can. If you can install your script, you can install Daemon::Daemonize or Proc::Daemon. – ikegami Aug 10 '15 at 14:01
  • Making my script was simply putting an rc1 file into a folder, it's just a bash script with a list of commands. There was no install needed, unless you're referring to something else? – Spellchamp Aug 10 '15 at 14:07
  • @Spellchamp: Please check the question I've linked. [Bklyn's answer](http://stackoverflow.com/a/768448/257635) shows how to do it using core modules. That might help you as you're unable to install new modules. – Chankey Pathak Aug 10 '15 at 15:49
  • So I implemented that daemonize sub, but it still isn't running after a reboot. It's like the daemon is starting no problem, but something is stopping it before the boot is finished. Thanks for your help so far. – Spellchamp Aug 11 '15 at 08:46
  • If you wish to add a new service to start when the machine boots you should add the necessary script to the directory /etc/init.d/. Many of the scripts already present in that directory will give you an example of the kind of things that you can do. – Chankey Pathak Aug 11 '15 at 09:09
  • So since switching from using daemonize, I can see the errors, and it seems that I was wrong, and that the script isn't running at all. Instead I get "Can't locate strict.pm in @INC" which I find odd, since the script runs fine when the machine has booted fully. Also my linux version isn't in a persistent state, so using init.d is an issue for me. – Spellchamp Aug 11 '15 at 10:55
1

The problem was that @INC hadn't fully loaded all of the modules by the time my script was called, and so my daemon wasn't running. I used the PERL5LIB command to add the appropriate directories.

PERL5LIB='/perl:/custom/lib:/usr/local/lib64/perl5' /var/myfolder/myscript.pl &

where /perl; /custom/lib and /usr/local/lib64/perl5 are the directories which were missing from @INC

Spellchamp
  • 295
  • 4
  • 16