Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
linux uptime



ocate
New User

Jan 13, 2016, 9:01 PM


Views: 20238
linux uptime

hi guys, i want to know how to call the uptime of linux in seconds so that i can make a program that will notify me if i exceed 30days uptime.


Laurent_R
Veteran / Moderator

Jan 19, 2016, 2:54 PM


Views: 20109
Re: [ocate] linux uptime

using the backticks of the qx// quote-like operator should enable you to call a bash command and retrieve the print-out of that command. Something like this, assuming that the Linux command is "uptime":

Code
my $up = `uptime`;



ocate
New User

Jan 19, 2016, 5:18 PM


Views: 20100
Re: [Laurent_R] linux uptime

#!/usr/bin/perl
while(1) {

$LinCtime = int (`cat /proc/uptime | awk '{print \$1}'`);
$LinCtimesec =$winCtime%60;
$LinCtimemin =int($LinCtime/60)%60;
$LinCtimehour=int($LinCtime/3600)%24;
$LinCtimeday =int($LinCtime/86400)%24;

print "THE SYSTEM HASS BEEN UP FOR
$LinCtimeday days
$LinCtimehour hours
$LinCtimemin minutes
$LinCtimesec seconds\n";
sleep (1);
system("cls")

if ($LinCtimeday >=30){
printf "THE COMPUTER NEEDS TO REBOOT!!\n";
open FILE, "user.txt" or die $!;
exit;

} else {
}
}


i already figured it out </3

In Reply To


FishMonger
Veteran / Moderator

Jan 20, 2016, 7:53 AM


Views: 20072
Re: [ocate] linux uptime

Hmm, a shell script disguised as a perl script.


Laurent_R
Veteran / Moderator

Jan 22, 2016, 1:51 PM


Views: 20004
Re: [ocate] linux uptime

This is really uselessly complicated.

Try perhaps to build on something like this:


Code
use strict; 
use warnings;

my $thirty_days = 60 * 60 * 24 * 30; # 30 days in seconds
my $time-output = `cat /proc/uptime`;
my $uptime = (split / /, $time-output)[0];
if ($uptime > $thirty_days) {
print "The server has been up for more than 30 days.\n";
}



FishMonger
Veteran / Moderator

Jan 22, 2016, 3:05 PM


Views: 19998
Re: [Laurent_R] linux uptime

Or, even a more perlish approach.


Code
use strict; 
use warnings;
use Unix::Uptime;

my $thirty_days = 60 * 60 * 24 * 30;

if (Unix::Uptime->uptime > $thirty_days) {
print "uptime is greater than 30 days\n";
}



ocate
New User

Jan 22, 2016, 4:44 PM


Views: 19991
Re: [FishMonger] linux uptime

is there a convertion so that i can make it an executable file?

In Reply To


FishMonger
Veteran / Moderator

Jan 23, 2016, 7:10 AM


Views: 19968
Re: [ocate] linux uptime

You can use pp (par packer) to create a standalone executable.
http://search.cpan.org/~rschupp/PAR-Packer-1.029/lib/pp.pm


ocate
New User

Jan 25, 2016, 5:48 PM


Views: 19852
Re: [FishMonger] linux uptime

i need help.. i cant run my script in crontab -e .. im not sure if im in the correct format or is there another way how run it?

*/2 * * * * perl ./f.pl


Laurent_R
Veteran / Moderator

Jan 26, 2016, 2:13 AM


Views: 19832
Re: [ocate] linux uptime

Is your script running OK when launched from the prompt?

Quite often, running something via the crontab creates problems due to environment factors (environment variables, etc.) because the crontab user is not the same and some configuration is differernt. But this is really a system question rather than a Perl question. And it will be difficult to help you, because we don't have access to your environment and can hardly guess what can go wrong.


FishMonger
Veteran / Moderator

Jan 26, 2016, 6:26 AM


Views: 19827
Re: [ocate] linux uptime

You need to use the full system paths. Relative paths don't work in cron jobs.


Code
*/2 * * * * /usr/bin/perl /path/to/f.pl


if the script has a properly configured shebang line, you don't need that path in the cron job.


Code
*/2 * * * * /path/to/f.pl