sys-uptime.txt
6KB · 213 lines
[SE] �ý��� ���� �ð��� �ð� �˾ƺ���
- �ۼ��� : ��ĥ�� <san2(at)linuxchannel.net>
- �ۼ��� : 2002.11.09
- �� �� : �ý��۰���(SE)
- �� �� : �ʺ�
- �� �� : �ý��� ���� �ð�(���)�� �ð��� �˾ƺ��� ��
- ���� : uptime, last, /proc/uptime, /proc/stat, perl, PHP
*��)
�� ������ ���� �ֽ� ���� �Ʒ� URL���� Ȯ���� �� �ֽ��ϴ�.
http://www.linuxchannel.net/docs/sys-uptime.txt
[��1] uptime ��ɾ�
���� ������ ���Դϴ�.
# uptime
9:01pm up 2:12, 1 user, load average: 0.00, 0.00, 0.00
#
��� �� �ý���� ������ 2�ð� 12�� ��������.. ���� ���� 9�� 01�к��� ������ �Ž���
�ö� ���� �ð��� �� �� �ְ��.... (������ ������. T.T)
[��2] last ��ɾ�
# last | grep boot | head -n 1
reboot system boot 2.4.19-R2 Sat Nov 9 18:50 (02:12)
#
���� �������� (02:12) ���� �ý��� uptime �Դϴ�. �� 2�ð� 12�� �����ٴ� �ǹ�Դϴ�.
���� 'Sat Nov 9 18:50'�� �ý��� boot time �Դϴ�.
[��3] /proc/uptime �̿�
# cat /proc/uptime
7979.84 7961.31
#
ù��° ���� �ý���� ������ ��� �ð����� ���� �ð��� �ʴ���� ǥ���� sytem uptime
���Դϴ�. �ι�° ���� 'idle uptime' �Դϴ�.
(reference : /usr/src/linux/fs/proc/proc_misc.c)
���� 7979 �ʸ� ����ϴ� �ý���� ���õ�� �� 2�ð� 13��̵Ǿ�����.
(uptime ��ɾ�� Ȯ���ϸ� ��� ���� ����)
�� ���� ��Ȯ�ϰ� ���� �ð��� ����� ���ô�.
������ ���� �ð��� 7979��̹Ƿ� ��� UNIX timestamp ������ 7979�ʸ� ����
���ý���� Unix timestamp ���� �����.. �� ���� perl�� gmdate()�̳� PHP�� data()
�Լ��� �̿��ϸ� ���� ���ý���� �ð��� �� �� �ֽ��ϴ�(C�� ����).
-- uptime.pl -------------------------------------------------------
#!/usr/bin/perl
#
#
open(UPTIME, '/proc/uptime') or die "Can't open /proc/uptime\n";
while(<UPTIME>) {
@uptime = $_; ## to array
}
close(UPTIME);
$btime = time() - int $uptime[0];
$btime = gmtime($btime + (3600*9)); ## GMT+9
print "boot time : $btime\n";
exit 0;
-------------------------------------------------------------------
(���� ��)
# perl uptime.pl
boot time : Sat Nov 9 18:49:55 2002
#
-- uptime.php ------------------------------------------------------
<?php
$uptime = file('/proc/uptime');
$btime = time() - (int)$uptime[0];
echo 'boot time: ' . date('Y.m.d H:i:s',$btime);
?>
-------------------------------------------------------------------
<�> �ǽð�(1�ʰ���)���� uptime ����
# watch -n 1 cat /proc/uptime
����� 'Ctrl + C'
</�>
<���> PHP sysinfo(http://ftp.linuxchannel.net/devel/sysinfo.tar)
�ֱ� �������� boot time�� �߰��Ǿ����ϴ�.
</���>
[��4] /proc/stat �̿�
# cat /proc/stat
cpu 1221 0 639 767372
cpu0 1221 0 639 767372
page 36710 5580
swap 1 0
intr 785185 769232 9 0 0 0 1 3 0 0 7363 8173 0 356 0 0 48
disk_io: (8,0):(3697,2449,73190,1248,11048)
ctxt 177800
btime 1036835394
processes 753
#
8��° ��� btime ��(1036835394)�� �ٷ� ���ý�� UNIX timestamp ���Դϴ�.
�� ���� [��3]���� ����� $btime ���� �����ϴ�(���1��).
�ٲپ� ���ϸ� ��� UNIX timestamp ������ �� btime ���� ���� �ý��� uptime ����
���ɴϴ�. �� uptime ���� [��3]���� �� $uptime[0] ���� �����ϴ�.
�̹����� �� btime ���� �� boot time�� ����ϰ� uptime ���� ����Ͽ� �б�
���ϰ� ����� ���ô�.
-- uptime2.pl -------------------------------------------------------
#!/usr/bin/perl
#
# reference : http://www.blackplasma.net/scripts/load
#
open(STAT, '/proc/stat') or "Can't open /proc/stat\n";
$i = 0;
while(<STAT>) {
$stat[$i] = $_;
$i++;
}
close(STAT);
$stat[7] =~ s/[^\d]+//g; ## change boot time(UNIX timestamp)
$btime = gmtime($stat[7] + (3600*9));
$term = time() - $stat[7];
$uptime = &runtime($term);
print "boot time\t: $btime\n";
print "system uptime\t: $uptime\n";
sub runtime
{
my($term) = @_;
$days = int($term / 86400);
$secs = int($term % 86400);
$hours = int($secs / 3600);
$secs = int($secs % 3600);
$mins = int($secs / 60);
$secs = int($secs % 60);
$days = $days ? "${days}days" : '';
$hours = $hours ? " ${hours}hours" : '';
$mins = $mins ? " ${mins}minutes " : '';
$secs .= 'seconds';
return "$days$hours$mins$secs";
}
exit 0
-------------------------------------------------------------------
(��... perl�� ���� ��Ʊ���... T.T)
(���� ��)
# perl uptime2.pl
boot time : Sat Nov 9 18:49:54 2002
system uptime : 2hours 25minutes 29seconds
#
-- uptime2.php ------------------------------------------------------
<?php
$stat = file('/proc/stat');
$stat[7] = preg_replace('/[^\d]+/','',$stat[7]);
$btime = date('Y.m.d H:i:s',$stat[7]);
$uptime = runtime(time()-$stat[7]);
echo <<<_EOF_
boot time\t: $btime<BR>
system uptime\t: $uptime<BR>
_EOF_;
function runtime($term, $lang='kr')
{
$l[kr] = $l[ko] = array('��','��','�ð�','��','��');
$l[en] = array('seconds','minutes','hours','days','mon');
$days = (int)($term / 86400);
$secs = (int)($term % 86400);
$hours = (int)($secs / 3600);
$secs = (int)($secs % 3600);
$mins = (int)($secs / 60);
$secs = (int)($secs % 60);
$days = $days ? $days.$l[$lang][3] : '';
$hours = $hours ? " $hours".$l[$lang][2] : '';
$mins = $mins ? " $mins".$l[$lang][1].' ' : '';
$secs .= $l[$lang][0];
return "$days$hours$mins$secs";
}
?>
-------------------------------------------------------------------
[��5] �� �ܿ� ���ý� ����/���ŵǴ� �α� ���� Ȯ��
# ls -l --full-time /var/log/dmesg
-rw-r--r-- 1 root root 6867 Sat Nov 09 18:50:17 2002 /var/log/dmesg
#
EOF