目前共有9篇帖子。 内容转换:不转换▼
 
点击 回复
2396 8
ubuntu12.04管理开机启动项目的方法
一派护法 十九级
1楼 发表于:2012-8-15 14:23
alt ctrl t 打开终端
gnome-session-properties
一派护法 十九级
2楼 发表于:2012-8-15 14:24
一派护法 十九级
4楼 发表于:2012-8-15 15:16
使用这种方法添加启动项要注意一个问题:
开机启动后,这个程序的工作目录为home/用户名,并不是程序所在目录!(这和windows下是一样的)
如果程序运行时输出了文件,则不能直接添加该程序到启动项中,应该添加sh文件。
一派护法 十九级
5楼 发表于:2012-8-15 15:18
以以下程序为例,源代码:
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string>
#include <memory.h>

using namespace std;

int main()
{
    //获取星期数
    string month_en[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    string month_zh[]={"一","二","三","四","五","六","日"};
    int i;
    time_t t=time(0);
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%A",localtime(&t)); //获取星期英文字符串
    for (i=0;i<7;i++)
        if (month_en[i]==tmp)
            break;
    string str="%Y-%m-%d %X 星期";
    str+=month_zh[i];
    str+=" 开机 [本年第%j天 %z]\r\n";
   
    //显示其他元素
    memset(tmp,0,64);
    strftime(tmp,sizeof(tmp),str.c_str(),localtime(&t)); //tmp变量为最终要写入文件的字符串
    cout<<"Welcome!"<<endl;
    cout<<tmp;
   
    //写入文件
    char filename[20];
    strftime(filename,sizeof(filename),"powerlog_%Y.txt",localtime(&t)); //根据年份生成文件名
    FILE* file=fopen(filename,"a"); //打开文件
    fwrite(tmp,strlen(tmp),1,file); //应该用strlen而不是sizeof,否则会乱码
    fclose(file);
   
    return 0;
}

用g++编译后,不应该直接添加该程序为启动项。要先建立一个sh文件,这里我的文件名是autorun.sh:
#!/bin/bash
cd /home/octopus/程序/huber/powerlog
./powerlog

然后添加该sh文件为启动项,然后重启/注销,会发现程序目录下多了个powerlog_2012.txt文件
一派护法 十九级
6楼 发表于:2012-8-15 15:20
回复:5楼
如果是直接添加程序为启动项而不是sh文件的话,那么重启/注销,会发现新增的txt文件在home/用户名目录中。
(windows下也是这样,只不过不是home/用户名目录下)

一派护法 十九级
7楼 发表于:2012-8-15 15:22
【补充】
其实用不着打开终端,先点最右上角的关机按钮再点“启动应用程序”也行。
一派护法 十九级
8楼 发表于:2012-8-15 15:24
一派护法 十九级
9楼 发表于:2013-7-8 12:28
英文版本:
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string>
#include <memory.h>

using namespace std;

int main()
{
    time_t t=time(0);
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%A",localtime(&t)); //Day of week
    string str="%Y-%m-%d %X ";
    str+=tmp;
    str+=" [#%j %z] POWER ON\r\n";
  
    memset(tmp,0,64); //reset the buffer
    strftime(tmp,sizeof(tmp),str.c_str(),localtime(&t)); //Now tmp is the string to be written into the file and to be printed to the screen
    cout<<"Welcome!"<<endl;
    cout<<tmp;
  
    //Write
    char filename[20];
    strftime(filename,sizeof(filename),"powerlog_%Y.txt",localtime(&t)); //filename
    FILE* file=fopen(filename,"a"); //Open or create the file
    fwrite(tmp,strlen(tmp),1,file); //use strlen instead of sizeof
    fclose(file);
  
    return 0;
}

makefile:
powerlog: powerlog.cpp
    g++ powerlog.cpp -o powerlog
一派护法 十九级
10楼 发表于:2015-2-21 23:58
string month_es[]={"Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo"};


回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:2396 回复数:8
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2015-2-21 23:58
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。