帐前卒专栏

Without software, we are nothing.

Own Timer Replace Crontab

Sometime crontab in Linux or Unix can not run some program. Because the environment of crontab is not as same as that of system. If you want to know the difference between those two environments settings. look up /etc/cron.d/anacron file and ~/.profile and ~/.bashrc and xxxx. I can not remember all of them. /etc/cron.d/anacron file is just for crontab. ~/.profile and ~/.bashrc is for system. If you want to know more, search “linux environment file” in Google.

I think to find all difference between two environment is waste time obviously. Another method is write own timer codes. So Here is my solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
/**
 * @author chico chen chillycreator #@@& gmail.com
 */
void usage(){
  cout << "timer"<<endl;
  cout << "input your time point:[minute] [hour] [innerday] [innerweek]"<<endl;
  cout << "input your program name:[your program]"<<endl;
  cout << "all of input should be more than zero!"<<endl;
  cout << "you should run this program earlier than you wanted."<<endl;
  cout << "or else your program will run at next time point."<<endl;
}

int main(){

  // user input
  int min, hour, howday, howweek;
  string program;
  cout<< "input [min] [hour] [innerdays] [innerweeks]"<<endl;
  cin >> min >> hour >> howday >> howweek;
  cout <<"input your program"<<endl;
  cin >> program;
  if (program.size() <= 0 || min < 0 || hour < 0 || howday < 0 || howweek < 0){
      usage();
      return 1;
  }
  cout << "your timer will run program at "<<hour<<":"<<min<<endl;
  cout << "your program is "<<program<<endl;
  if (0 == howday || 1 == howday){
      howday = 1;
      cout << "run this program in every day"<<endl;
  }
  else{
      cout << "run this program at "<<howday <<" days intervals."<<endl;
  }
  if (0 == howweek){
      cout << "run this program in every week"<<endl;
  }
  else{
      cout << "run this program at "<<howweek <<" weeks intervals."<<endl;
  }
  // set sleep time and loop time
  int looptime = (howday-1)*3600*24 + howweek * 7 * 3600 * 24;
  int sleeptime = 0;
  bool bFirst = true;
  while(true){
      time_t now;
      time(&now);
      struct tm * timeinfo = localtime(&now);
      timeinfo->tm_hour = hour;
      timeinfo->tm_min = min;
      time_t futuretime = mktime(timeinfo);
      sleeptime = futuretime - now;
      sleeptime = sleeptime >= 0 ? sleeptime : 24*3600+sleeptime;
      if (!bFirst){
          sleeptime += looptime;
      }
      bFirst = false;
      cout << "wait time:"<<sleeptime<<endl;
      sleep(sleeptime);
      cout << "start to run:"<<program<<endl;
      system(program.c_str());
      cout << "end program..."<<endl;
  }
  // loop end
  return 0;
}

Write your own code will bring new bugs in your solution. Another drawback is spending your own time and this timer is not in common. However, you can control it!

Comments