Linux 配合远程服务器升级程序

#ifndef UPGRADE_H
#define UPGRADE_H

#include <string>

using namespace std;

class upgrade {
public:
	string url; //升级程序路径
	string version; //版本对比
	upgrade()
	{
	}
	~upgrade()
	{
	}
	int main(void);
};

#endif


#include <iostream>
#include <string>

#include "upgrade.h"

using namespace std;

#define DEBUG 1

int upgrade::main(void)
{
	// 执行upgrade.sh,进行更新
	// 成功失败都重启
	int maxline = 512;
	char buf[maxline] = {0};
#ifdef DEBUG
	snprintf(buf, maxline, "sh -x upgrade.sh %s", url.c_str());
#else
	snprintf(buf, maxline, "sh upgrade.sh %s", url.c_str());
#endif
	FILE *f = popen(buf, "r");
    fread(buf, 1, maxline-1, f);
	pclose(f);
	if(strstr(buf, "OK") != NULL) {
		cout<<"升级成功,重启中。。。"<<endl;
		return 0;
	}

	cout<<buf<<endl;
	cout<<"升级失败,重启中。。。"<<endl;

	return -1;
}


upgrade.sh
#!/bin/bash

if [ -z "$1" ]
then
	echo "Usage: ./upgrade.sh <url>"
	exit 1
fi

rm -rf /tmp/update /tmp/update-*.tar /tmp/update-*.tar.xz

#wget -P /tmp -o /dev/null "$1"
wget -P /tmp "$1"
[ $? -ne 0 ] && echo "failed" && exit -1

cd /tmp
xz -d update-*.tar.xz
[ $? -ne 0 ] && echo "failed" && exit -1
tar -xf update-*.tar
[ $? -ne 0 ] && echo "failed" && exit -1

cd -
[ $? -ne 0 ] && echo "failed" && exit -1
cp -af /tmp/update/* `dirname $0`/ # 程序默认放在此目录下
[ $? -ne 0 ] && echo "failed" && exit -1
chmod a+x `dirname $0`/test
chmod a+x `dirname $0`/startauto.sh
chmod a+x `dirname $0`/upgrade.sh
[ $? -ne 0 ] && echo "failed" && exit -1
echo OK

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值