Periodically check if you need to restart the process with Crontab and Perl

I wrote a simple script in perl to check if my server is working. If it is not, the script will run it again. This is the script:

#!/usr/bin/perl -w use strict; use warnings; my($command, $name) = ("/full_path_to/my_server", "my_server"); if (`pidof $name`){ print "Process is running!\n"; } else{ `$command &`; } 

The scripts work fine when I manually run it, but when I run it in crontab, it cannot find the dynamic libraries used by the server, located in the same folder.

Entrance to Crontab:

 */5 * * * * /usr/bin/perl -w /full_path_to_script/autostartServer 

I assume this is a context issue in which the application starts. What is the smart way to solve this problem?

+6
source share
1 answer

A simple solution is to remove the full path in the command and execute "cd / path" before executing the command. Thus, it will be launched in the same folder as the libraries. The code will look like this:

 #!/usr/bin/perl -w use strict; use warnings; my($command, $name) = ("./my_server", "my_server"); if (`pidof $name`) { print "Process is running!\n"; } else { `cd /full_path_to`; `$command &`; } 
+5
source

Source: https://habr.com/ru/post/959243/


All Articles