作者共發了9篇帖子。 內容轉換:不轉換▼
 
點擊 回復
2330 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)
 

本帖信息

點擊數:2330 回複數:8
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-2-21 23:58
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。