LINUX 看門狗
阿新 • • 發佈:2019-01-26
先要編寫看門狗程式program.sh,內容如:
#!/bin/bash
#注:本指令碼需要以超級使用者身份執行。
# 監測的時間間隔,秒計
INTERVAL=60
# 重啟時間間隔
INTERVALRESTART=1
#==================================================================
PROGRAME=program
THREADNUMS=1
LOG=/apps/server/monitor.log
#SYSDATE=$(date)
SYSBUILD=`/bin/cat /etc/redhat-release | /bin/awk '{print $5$7}'`
var="-emf"
case ${SYSBUILD} in
"7.3")
var="-e"
;;
"8.0")
var="-em"
;;
"AS3")
var="-emf"
;;
esac
while true
do
SYSDATE=$(date)
nowps1=`ps $var | grep $PROGRAME | grep -v grep | wc -l`
nowps1=`expr $nowps1`
if [ $nowps1 -lt $THREADNUMS ]; then
/bin/sh /apps/program.sh
echo " " >> $LOG
echo "*******************************************************" >> $LOG
echo "Restart time:" $SYSDATE >> $LOG
echo "---------- Program $PROGRAME restart ----------------" >> $LOG
echo "*******************************************************" >> $LOG
echo " "
fi
sleep $INTERVAL
done
#-----------------------------------------------------------
exit 0
在檔案/etc/rc.local在其中增加一個啟動項,即在檔案末尾增加一條可執行語句(如:/bin/sh /apps/program.sh)
看門狗製作完畢。
看門狗在嵌入式系統開發中佔據重要的地位,管理系統的工作狀態。在這裡本人muge0913在參考別人的基礎上,實現了mini6410看門狗的移植。
在mini6410中看門狗驅動檔案為linux2.6.38/drivers/watchdog/s3c2410_wdt.c
在mini6410中linux系統預設看門狗是不開機啟動,但是我們可以向/dev/watchdog寫入資料來啟動或關閉看門狗。
如:echo 0 >/dev/watchdog
echo這個命令啟動的作用是先開啟檔案,再寫入內容,然後關閉。也就是open->write->release。
一段時間後系統會自動重啟。
如果執行:
echo 0 >/dev/watchdog
echo V >/dev/watchdog
系統側不會重啟。
原因分析:
open函式:
static int s3c2410wdt_open(struct inode *inode, struct file *file)
{
if (test_and_set_bit(0, &open_lock))
return -EBUSY;
if (nowayout)
__module_get(THIS_MODULE);
expect_close = 0;
/* start the timer */
s3c2410wdt_start();
return nonseekable_open(inode, file);
}
release函式:
static int s3c2410wdt_release(struct inode *inode, struct file *file)
{
/*
* Shut off the timer.
* Lock it in if it's a module and we set nowayout
*/
if (expect_close == 42)
s3c2410wdt_stop();
else {
dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
s3c2410wdt_keepalive();
}
expect_close = 0;
clear_bit(0, &open_lock);
return 0;
}
write函式:
static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
/*
* Refresh the timer.
*/
if (len) {
if (!nowayout) {
size_t i;
/* In case it was set long ago */
expect_close = 0;
for (i = 0; i != len; i++) {
char c;
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
expect_close = 42;
}
}
s3c2410wdt_keepalive();
}
return len;
}
看門狗只能被一個程序開啟,開啟函式中先判斷了一下,然後啟動了看門狗;再看write函式,寫入的如果是V則允許關閉看門狗,如果不是V僅僅喂狗一次;最後是release函式,如果允許關閉則關閉看門狗,如果不允許關閉,列印"Unexpectedclose, not stoppingwatchdog",喂狗一次。此時看門狗並沒有關閉,所以系統會復位的,如果輸入V則看門狗被關閉,這樣系統就不復位了。
看門狗在mini6410上的移植過程:
首先配置:make menuconfig
在drivers/watchdog/s3c2410_wdt.c中進行修改:
#define CONFIG_S3C2410_WATCHDOG_ATBOOT (1)
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
注:
#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)//系統啟動時不開啟看門狗
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)//復位時間
設定成系統啟動就啟動看門狗,並且看門狗到期時間為15s。這樣系統復位後每15s系統就會復位一次,所以我們在使用者空間進行喂狗,驅動中的那個中斷函式是當看門狗作為定時器時用的,所以沒有實現喂狗,所以只能在使用者程式中喂狗,下面是原始碼:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/watchdog.h>
int main(int argc, char **argv){
int fd;
fd = open("/dev/watchdog",O_RDONLY);
if(fd < 0){
perror("/dev/watchdog");
return -1;
}
for(;;){
ioctl(fd, WDIOC_KEEPALIVE);
sleep(3);
}
close(fd);
return 0;
}
編譯:
arm-linux-gcc wdt.c -o wdt
把wdt拷貝到root-2.6.30.4/sbin/下,並修改root-2.6.38/etc/init.d/rcS檔案,新增wdt&這麼一句,讓系統啟動後這個應用程式在後臺執行。
#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
#
# Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#
trap ":" INT QUIT TSTP
/bin/hostname FriendlyARM
[ -e /proc/1 ] || /bin/mount -n -t proc none /proc
[ -e /sys/class ] || /bin/mount -n -t sysfs none /sys
[ -e /dev/tty ] || /bin/mount -t ramfs none /dev
/bin/mount -n -t usbfs none /proc/bus/usb
echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
/bin/hotplug
# mounting file system specified in /etc/fstab
mkdir -p /dev/pts
mkdir -p /dev/shm
/bin/mount -n -t devpts none /dev/pts -o mode=0622
/bin/mount -n -t tmpfs tmpfs /dev/shm
/bin/mount -n -t ramfs none /tmp
/bin/mount -n -t ramfs none /var
mkdir -p /var/empty
mkdir -p /var/log
mkdir -p /var/lock
mkdir -p /var/run
mkdir -p /var/tmp
/sbin/hwclock -s
syslogd
/etc/rc.d/init.d/netd start
echo " " > /dev/tty1
echo "Starting networking..." > /dev/tty1
sleep 1
/etc/rc.d/init.d/httpd start
echo " " > /dev/tty1
echo "Starting web server..." > /dev/tty1
sleep 1
/etc/rc.d/init.d/leds start
echo " " > /dev/tty1
echo "Starting leds service..." > /dev/tty1
echo " "
sleep 1
echo " " > /dev/tty1
/etc/rc.d/init.d/alsaconf start
echo "Loading sound card config..." > /dev/tty1
echo " "
/sbin/ifconfig lo 127.0.0.1
/etc/init.d/ifconfig-eth0
/sbin/wdt&
/bin/qtopia &
echo " " > /dev/tty1
echo "Starting Qtopia, please waiting..." > /dev/tty1
http://www.linuxso.com/linuxbiancheng/12702.html
某CSDN帖子
/dev/watchdog是一個字元裝置節點,簡單點可以理解為linux下的一個檔案,在程式中使用看門狗的過程大致分為以下幾步:
1.開啟年看門狗“檔案”;
2.設定超時時間;
3,週期性向這個檔案寫入字元(喂狗);
若程式出現異常退出,無法在喂狗週期內寫入字元,則系統將自動復位重啟。我寫的一個例子程式碼如下:
C/C++ code
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/watchdog.h>
#define WDT "/dev/watchdog"
int main()
{
int wdt_fd = -1;//看門狗檔案標識
int timeout = 10;//超時時間為10s
wdt_fd = open(WDT,O_WRONLY);
if(wdt_fd == -1)
{
printf("----------Fail to open "WDT"!\n");
}
ioctl(wdt_fd,WDIOC_SETTIMEOUT,&timeout);
ioctl(wdt_fd,WDIOC_GETTIMEOUT,&timeout);
while(1)
{
write(wdt_fd,"\0",1);
printf("feet dog***********\n");
sleep(9);
}
return 0;
}
轉自 http://www.blogjava.net/xixuui/archive/2007/06/27/126545.html
轉自 http://blog.csdn.net/zhiweiyouzhishenghuo/article/details/7744155
#!/bin/bash
#注:本指令碼需要以超級使用者身份執行。
# 監測的時間間隔,秒計
INTERVAL=60
# 重啟時間間隔
INTERVALRESTART=1
#==================================================================
PROGRAME=program
THREADNUMS=1
LOG=/apps/server/monitor.log
#SYSDATE=$(date)
SYSBUILD=`/bin/cat /etc/redhat-release | /bin/awk '{print $5$7}'`
var="-emf"
case ${SYSBUILD} in
"7.3")
var="-e"
;;
"8.0")
var="-em"
;;
"AS3")
var="-emf"
;;
esac
while true
do
SYSDATE=$(date)
nowps1=`ps $var | grep $PROGRAME | grep -v grep | wc -l`
nowps1=`expr $nowps1`
if [ $nowps1 -lt $THREADNUMS ]; then
/bin/sh /apps/program.sh
echo " " >> $LOG
echo "*******************************************************" >> $LOG
echo "Restart time:" $SYSDATE >> $LOG
echo "---------- Program $PROGRAME restart ----------------" >> $LOG
echo "*******************************************************" >> $LOG
echo " "
fi
sleep $INTERVAL
done
#-----------------------------------------------------------
exit 0
在檔案/etc/rc.local在其中增加一個啟動項,即在檔案末尾增加一條可執行語句(如:/bin/sh /apps/program.sh)
看門狗製作完畢。
看門狗在嵌入式系統開發中佔據重要的地位,管理系統的工作狀態。在這裡本人muge0913在參考別人的基礎上,實現了mini6410看門狗的移植。
在mini6410中看門狗驅動檔案為linux2.6.38/drivers/watchdog/s3c2410_wdt.c
在mini6410中linux系統預設看門狗是不開機啟動,但是我們可以向/dev/watchdog寫入資料來啟動或關閉看門狗。
如:echo 0 >/dev/watchdog
echo這個命令啟動的作用是先開啟檔案,再寫入內容,然後關閉。也就是open->write->release。
一段時間後系統會自動重啟。
如果執行:
echo 0 >/dev/watchdog
echo V >/dev/watchdog
系統側不會重啟。
原因分析:
open函式:
static int s3c2410wdt_open(struct inode *inode, struct file *file)
{
if (test_and_set_bit(0, &open_lock))
return -EBUSY;
if (nowayout)
__module_get(THIS_MODULE);
expect_close = 0;
/* start the timer */
s3c2410wdt_start();
return nonseekable_open(inode, file);
}
release函式:
static int s3c2410wdt_release(struct inode *inode, struct file *file)
{
/*
* Shut off the timer.
* Lock it in if it's a module and we set nowayout
*/
if (expect_close == 42)
s3c2410wdt_stop();
else {
dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
s3c2410wdt_keepalive();
}
expect_close = 0;
clear_bit(0, &open_lock);
return 0;
}
write函式:
static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
/*
* Refresh the timer.
*/
if (len) {
if (!nowayout) {
size_t i;
/* In case it was set long ago */
expect_close = 0;
for (i = 0; i != len; i++) {
char c;
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
expect_close = 42;
}
}
s3c2410wdt_keepalive();
}
return len;
}
看門狗只能被一個程序開啟,開啟函式中先判斷了一下,然後啟動了看門狗;再看write函式,寫入的如果是V則允許關閉看門狗,如果不是V僅僅喂狗一次;最後是release函式,如果允許關閉則關閉看門狗,如果不允許關閉,列印"Unexpectedclose, not stoppingwatchdog",喂狗一次。此時看門狗並沒有關閉,所以系統會復位的,如果輸入V則看門狗被關閉,這樣系統就不復位了。
看門狗在mini6410上的移植過程:
首先配置:make menuconfig
在drivers/watchdog/s3c2410_wdt.c中進行修改:
#define CONFIG_S3C2410_WATCHDOG_ATBOOT (1)
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
注:
#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)//系統啟動時不開啟看門狗
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)//復位時間
設定成系統啟動就啟動看門狗,並且看門狗到期時間為15s。這樣系統復位後每15s系統就會復位一次,所以我們在使用者空間進行喂狗,驅動中的那個中斷函式是當看門狗作為定時器時用的,所以沒有實現喂狗,所以只能在使用者程式中喂狗,下面是原始碼:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/watchdog.h>
int main(int argc, char **argv){
int fd;
fd = open("/dev/watchdog",O_RDONLY);
if(fd < 0){
perror("/dev/watchdog");
return -1;
}
for(;;){
ioctl(fd, WDIOC_KEEPALIVE);
sleep(3);
}
close(fd);
return 0;
}
編譯:
arm-linux-gcc wdt.c -o wdt
把wdt拷貝到root-2.6.30.4/sbin/下,並修改root-2.6.38/etc/init.d/rcS檔案,新增wdt&這麼一句,讓系統啟動後這個應用程式在後臺執行。
#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
#
# Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#
trap ":" INT QUIT TSTP
/bin/hostname FriendlyARM
[ -e /proc/1 ] || /bin/mount -n -t proc none /proc
[ -e /sys/class ] || /bin/mount -n -t sysfs none /sys
[ -e /dev/tty ] || /bin/mount -t ramfs none /dev
/bin/mount -n -t usbfs none /proc/bus/usb
echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
/bin/hotplug
# mounting file system specified in /etc/fstab
mkdir -p /dev/pts
mkdir -p /dev/shm
/bin/mount -n -t devpts none /dev/pts -o mode=0622
/bin/mount -n -t tmpfs tmpfs /dev/shm
/bin/mount -n -t ramfs none /tmp
/bin/mount -n -t ramfs none /var
mkdir -p /var/empty
mkdir -p /var/log
mkdir -p /var/lock
mkdir -p /var/run
mkdir -p /var/tmp
/sbin/hwclock -s
syslogd
/etc/rc.d/init.d/netd start
echo " " > /dev/tty1
echo "Starting networking..." > /dev/tty1
sleep 1
/etc/rc.d/init.d/httpd start
echo " " > /dev/tty1
echo "Starting web server..." > /dev/tty1
sleep 1
/etc/rc.d/init.d/leds start
echo " " > /dev/tty1
echo "Starting leds service..." > /dev/tty1
echo " "
sleep 1
echo " " > /dev/tty1
/etc/rc.d/init.d/alsaconf start
echo "Loading sound card config..." > /dev/tty1
echo " "
/sbin/ifconfig lo 127.0.0.1
/etc/init.d/ifconfig-eth0
/sbin/wdt&
/bin/qtopia &
echo " " > /dev/tty1
echo "Starting Qtopia, please waiting..." > /dev/tty1
http://www.linuxso.com/linuxbiancheng/12702.html
某CSDN帖子
/dev/watchdog是一個字元裝置節點,簡單點可以理解為linux下的一個檔案,在程式中使用看門狗的過程大致分為以下幾步:
1.開啟年看門狗“檔案”;
2.設定超時時間;
3,週期性向這個檔案寫入字元(喂狗);
若程式出現異常退出,無法在喂狗週期內寫入字元,則系統將自動復位重啟。我寫的一個例子程式碼如下:
C/C++ code
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/watchdog.h>
#define WDT "/dev/watchdog"
int main()
{
int wdt_fd = -1;//看門狗檔案標識
int timeout = 10;//超時時間為10s
wdt_fd = open(WDT,O_WRONLY);
if(wdt_fd == -1)
{
printf("----------Fail to open "WDT"!\n");
}
ioctl(wdt_fd,WDIOC_SETTIMEOUT,&timeout);
ioctl(wdt_fd,WDIOC_GETTIMEOUT,&timeout);
while(1)
{
write(wdt_fd,"\0",1);
printf("feet dog***********\n");
sleep(9);
}
return 0;
}
轉自 http://www.blogjava.net/xixuui/archive/2007/06/27/126545.html
轉自 http://blog.csdn.net/zhiweiyouzhishenghuo/article/details/7744155