CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



ocate
New User

Jan 13, 2016, 9:01 PM

Post #1 of 11 (13106 views)
linux uptime Can't Post

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

Post #2 of 11 (12977 views)
Re: [ocate] linux uptime [In reply to] Can't Post

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

Post #3 of 11 (12968 views)
Re: [Laurent_R] linux uptime [In reply to] Can't Post

#!/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

Post #4 of 11 (12940 views)
Re: [ocate] linux uptime [In reply to] Can't Post

Hmm, a shell script disguised as a perl script.


Laurent_R
Veteran / Moderator

Jan 22, 2016, 1:51 PM

Post #5 of 11 (12872 views)
Re: [ocate] linux uptime [In reply to] Can't Post

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

Post #6 of 11 (12866 views)
Re: [Laurent_R] linux uptime [In reply to] Can't Post

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

Post #7 of 11 (12859 views)
Re: [FishMonger] linux uptime [In reply to] Can't Post

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

Post #8 of 11 (12836 views)
Re: [ocate] linux uptime [In reply to] Can't Post

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

Post #9 of 11 (12720 views)
Re: [FishMonger] linux uptime [In reply to] Can't Post

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

Post #10 of 11 (12700 views)
Re: [ocate] linux uptime [In reply to] Can't Post

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

Post #11 of 11 (12695 views)
Re: [ocate] linux uptime [In reply to] Can't Post

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


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives