Changes

3,513 bytes added ,  10:35, 7 September 2021
Line 3: Line 3:  
'''Crontab''' is a list of commands that allows tasks (programs, scripts) to be run automatically at regular time intervals. For example, in RUTxxx routers it is responsible for executing such services as Automatic Reboot functions, SIM Idle Protection, Scheduled SMS and other functions and services that operate in a periodic manner.  
 
'''Crontab''' is a list of commands that allows tasks (programs, scripts) to be run automatically at regular time intervals. For example, in RUTxxx routers it is responsible for executing such services as Automatic Reboot functions, SIM Idle Protection, Scheduled SMS and other functions and services that operate in a periodic manner.  
   −
The crontab can be opened for editing, adding, removing or modifying scheduled tasks. This article will provide an explanation of the crontab functionality principle and present some usage examples in hopes that it may help you configure your own crontab rules.
+
The crontab can be opened for editing, adding, removing or modifying scheduled tasks. This article will provide an explanation of the crontab functionality principle and present some usage examples with the hope that it may help you configure your own crontab rules.
    
==Crontab syntax and editing overview==
 
==Crontab syntax and editing overview==
Line 82: Line 82:  
| style="text-align: center; vertical-align: top;" | *
 
| style="text-align: center; vertical-align: top;" | *
 
| style="text-align: center; vertical-align: top;" | *
 
| style="text-align: center; vertical-align: top;" | *
| style="text-align: center; vertical-align: top;" | 5
+
| style="text-align: center; vertical-align: top;" | 1-5
 
| style="text-align: left; vertical-align: top;"  | At 6:25 AM every weekday (Mon-Fri)
 
| style="text-align: left; vertical-align: top;"  | At 6:25 AM every weekday (Mon-Fri)
 
|-
 
|-
Line 102: Line 102:     
There are several nuances to discuss here:
 
There are several nuances to discuss here:
* a '''number''' indicates the exact time at which an action should occur. For example, if the number '''5''' was placed in the minute section, the specified action would be taken at the fifth minute of every hour.
+
* a '''number''' indicates the exact time at which an action should occur. For example, if the number '''5''' was placed in the minute section, the specified action would be taken at the fifth minute of every specified hour.
* an '''asterisk''' ('''*''') stands for every possible value of the relevant time unit. For example, if placed in the hour section, it would indicate that an action should be taken every hour.
+
* an '''asterisk''' ('''*''') stands for every possible value of the relevant time unit. For example, if placed in the hour section, it would indicate that an action should be taken every hour on specified days.
* two numbers separated by a '''dash'''('''-''') indicate a range of numbers. For example, if '''4-7''' was placed in the month section, it would indicate than the specified action should take place every fourth, fifth, sixth and seventh month of a year.
+
* two numbers separated by a '''dash'''('''-''') indicate a range of numbers. For example, if '''4-7''' was placed in the month section, it would indicate than the specified action should take place every fourth, fifth, sixth and seventh months of a year.
* numbers separated by '''commas''' (''',''') indicate multiple specific time stamps at which an action should occur. For example, if '''6,9,10''' was placed in the hour section, the specified action would be taken at 6:00 AM, 9:00 AM and 10 AM every day. Ranges of numbers can also be separated by commas. For instance, '''5-10,20-25''' placed in the minute section would indicate that an action must occur every fifth through tenth minute as well as every twentieth through twenty fifth minute.
+
* numbers separated by '''commas''' (''',''') indicate multiple specific time stamps at which an action should occur. For example, if the numbers '''6,9,10''' were placed in the hour section, the specified action would be taken at 6:00 AM, 9:00 AM and 10 AM every day. Ranges of numbers can also be separated by commas. For instance, '''5-10,20-25''' placed in the minute section would indicate that an action must occur every fifth through tenth minute as well as every twentieth through twenty fifth minute of every specified hour.
 
* an asterisk and a number separated by a '''forward slash''' ('''/''') indicate a periodicity. For example, '''*/10''' placed in the minute section would indicate that the specified action should occur every 10 minutes.
 
* an asterisk and a number separated by a '''forward slash''' ('''/''') indicate a periodicity. For example, '''*/10''' placed in the minute section would indicate that the specified action should occur every 10 minutes.
 
* the '''day of week''' segment functions a bit differently than the others:
 
* the '''day of week''' segment functions a bit differently than the others:
Line 111: Line 111:  
** since weekdays shift throughout the year, i.e., they are not dependent on specific days of a month, when they are used in conjunction with crontab, the action will be executed when the current time matches the value in either field. For instance, '''5 5 5 * 5''' would cause the specified command to be executed at 5:05 AM every fifth day of every month plus every Friday.
 
** since weekdays shift throughout the year, i.e., they are not dependent on specific days of a month, when they are used in conjunction with crontab, the action will be executed when the current time matches the value in either field. For instance, '''5 5 5 * 5''' would cause the specified command to be executed at 5:05 AM every fifth day of every month plus every Friday.
 
----
 
----
To edit crontab, use '''crontab -e'''. This is analogous to using the '''vi''' command with the ''/etc/crontabs/root'' file, so the same rules  apply here as when editing with ''vi''. Type ''crontab -e'', press "Enter" and a text editor for the crontab file will open. To start editing, press the "I" key on your keyboard; you can then edit the crontab file much like with a regular text editor (except you can't use your mouse pointer to place the text cursor anywhere you want; use the arrow keys on your keyboard instead). To save changes after editing, press the "Escape" ("Esc") button on your keyboard, type ''':x''' and press "Enter". To cancel changes and exit the editor, press "'''Control'''" ('''Ctrl''') '''+''' "'''C'''" on your keyboard.
+
To edit crontab, use '''crontab -e'''. This is analogous to using the '''vi''' command on the ''/etc/crontabs/root'' file, so the same rules  apply here as when editing with ''vi''. Type ''crontab -e'', press "Enter" and a text editor for the crontab file will open. To start editing, press the "I" key on your keyboard; you can then edit the crontab file much like with a regular text editor (except you can't use your mouse pointer to place the text cursor anywhere you want; use the arrow keys on your keyboard instead). To save changes after editing, press the "'''Escape'''" ("'''Esc'''") button on your keyboard, type ''':x''' and press "Enter". To exit the editor without saving changes, press the "Escape" button and type ''':q!''' on your keyboard.
   −
A simpler yet more restricted method of editing would be using the '''echo''' command to add lines to the crontab file. It can be used in such a manner: '''echo "Hello, world" >> /etc/crontabs/root'''. This command will add the text '''Hello, world''' to the crontab file, i.e., it adds whatever is specified within the quotation marks (" ") after the ''echo'' command to the file that is specified after the double '''more than''' ('''>>''') symbol. For example, to add a rule that reboots the router everyday at midnight, we would have to use:
+
A simpler yet more restricted method of editing would be using the '''echo''' command to add new lines to the crontab file. It can be used in such a manner: '''echo "Hello, world" >> /etc/crontabs/root'''. This command will add the text '''Hello, world''' to the crontab file, i.e., it adds whatever is specified within the quotation marks (" ") after the ''echo'' command to the file that is specified after the double '''more than''' ('''>>''') symbol. For example, to add a rule that reboots the router everyday at midnight, we would have to use:
    
  # echo "0 0 * * * reboot" >> /etc/crontabs/root
 
  # echo "0 0 * * * reboot" >> /etc/crontabs/root
   −
Using the command in such a way simply adds a new line with the denoted text at the end of the specified file. This is convenient when you just want to add a new rule(s) quickly, but unlike ''vi'' or ''crontab -e'' it doesn't offer any other editing capabilities. So if you want edit or remove existing rules, this method will not offer a solution and ''crontab -e'' should be used instead.
+
Using the command in such a way simply adds a new line with the denoted text at the end of the specified file. This is convenient when you just want to add new rules quickly, but unlike ''vi'' or ''crontab -e'' it doesn't offer any other editing capabilities. So if you want edit or remove existing rules, this method will not offer a solution and ''crontab -e'' should be used instead.
    
==Examples==
 
==Examples==
   −
This section will provide some crontab usage examples in the hopes of helping you getting the hang of the system or even finding one that you could use. The examples provided here will be examined thoroughly but the editing method will not be discussed; you can read up on that in the section above.
+
This section will provide some crontab usage examples in the hopes of helping you to get the hang of the system or even finding an example that you can use. The examples provided here will be examined thoroughly but the editing method and environment will not be discussed; you can read up on those subjects in the section above.
    
===Periodic SIM switch===
 
===Periodic SIM switch===
Line 130: Line 130:     
Let's overview what each segment indicates sequentially:
 
Let's overview what each segment indicates sequentially:
* 45 - the action must take place at minute 45 of the hour
+
* 45 - the action must take place at minute 45 of the specified hour
 
* 18 - the action must take place at 6 PM
 
* 18 - the action must take place at 6 PM
 
* * - all days of the month are applicable to the rule
 
* * - all days of the month are applicable to the rule
Line 138: Line 138:  
* change - option for the sim_switch command
 
* change - option for the sim_switch command
   −
To sum up, the first five segments denote the frequency of the sim_switch command. The option '''change''' specifies that the SIM card that is currently in use will be switched. So the entire entry will perform a SIM switch to the opposite SIM card every weekday at 6:45 PM.
+
To sum up, the first five segments denote the frequency of the sim_switch command. The option '''change''' specifies that the SIM card that was in use up until the time of the switch will be switched to the one that was inactive up until the time of the switch. So the entire entry will perform a SIM switch to the opposite SIM card every weekday at 6:45 PM.
    
===Launching an OpenVPN server on specified hours===
 
===Launching an OpenVPN server on specified hours===
 
----
 
----
For this example we'll configure a system that launches an OpenVPN server at the start of a workday (8 AM) and shuts down said server at the end of a workday (6 PM). For this we'll need to add two rules. Each rule will require multiple commands to be executed. Crontab can launch multiple commands the same way as a single command, only the '''&&''' separator should be used to separate the different commands:  
+
For this example we'll configure a system that launches an OpenVPN server at the start of every workday (8 AM) and shuts down said server at the end of every workday (6 PM). For this we'll need to add two rules. Each rule will require multiple commands to be executed. Crontab can launch multiple commands the same way as a single command; the '''&&''' separator should be used to divide the different commands:  
   −
  0 8 * * 1-5 uci set openvpn.7365727665725F64656D6F.enable=1 && uci commit && /etc/init.d/openvpn restart
+
  0 8 * * 1-5 uci set openvpn.7365727665725F64656D6F.enable=1 && uci commit && /etc/init.d/openvpn start
  0 18 * * 1-5 uci set openvpn.7365727665725F64656D6F.enable=0 && uci commit && /etc/init.d/openvpn restart
+
  0 18 * * 1-5 uci set openvpn.7365727665725F64656D6F.enable=0 && uci commit && /etc/init.d/openvpn stop
    
Let's overview what each segment indicates sequentially:
 
Let's overview what each segment indicates sequentially:
* 0/0 - the action must take place at minute 0 (just as the hour changes)
+
* '''First line'''
* 8/18 - the action must take place at 8 AM and 6 PM respectively
+
** 0 - the action must take place at minute 0 (right after the hour has changed)
* */* - all days of the month are applicable to the rules
+
** 8 - the action must take place at 8 AM
* */* - all months of the year are applicable to the rules
+
** * - all days of the month are applicable to the rule
* 1-5 - the action must take place every Monday - Friday
+
** * - all months of the year are applicable to the rule
* openvpn.7365727665725F64656D6F.enable='''1/0''' - enables/disables OpenVPN
+
** 1-5 - the action must take place every Monday - Friday
* uci commit - commits configuration changes
+
** uci set openvpn.7365727665725F64656D6F.enable=1 - enables the OpenVPN instance
* /etc/init.d/openvpn restart - restarts OpenVPN service
+
** uci commit - commits configuration changes
* && - used to separate different commands
+
** /etc/init.d/openvpn restart - restarts the OpenVPN service
 +
* '''Second line'''
 +
** 0 - the action must take place at minute 0 (right after the hour has changed)
 +
** 18 - the action must take place at 6 PM
 +
** * - all days of the month are applicable to the rule
 +
** * - all months of the year are applicable to the rule
 +
** 1-5 - the action must take place every Monday - Friday
 +
** uci set openvpn.7365727665725F64656D6F.enable=0 - enables the OpenVPN instance
 +
** uci commit - commits configuration changes
 +
** /etc/init.d/openvpn restart - restarts the OpenVPN service
 +
* && - separates different commands
 +
 
 +
To sum up, the first line enables the OpenVPN server at 8 AM, the second disables it at 6 PM. At the end of the enabling and disabling processes the changes are committed and the related services restarted. We used UCI to change the state of the OpenVPN server. You can find more information on UCI in this article: '''[[UCI command usage]]'''.
 +
 
 +
'''NOTE''': different OpenVPN instances will have different path names (i.e., ''openvpn.7365727665725F64656D6F'') to the ''enable'' option. You can check the path with this command:
 +
 
 +
uci show openvpn
 +
 
 +
===Launching a WiFi Access Point (AP) on specified hours===
 +
----
 +
For this example we'll configure a system that launches a WiFi AP at the start of every workday (8 AM) and shuts down said AP at the end of every workday (6 PM). For this we'll need to add two rules. Each rule will require multiple commands to be executed. Crontab can launch multiple commands the same way as a single command; the '''&&''' separator should be used to divide the different commands:
 +
 
 +
0 8 * * 1-5 uci set wireless.@wifi-iface[0].user_enable=1 && uci delete wireless.@wifi-iface[0].disabled && uci commit && wifi
 +
0 18 * * 1-5 uci set wireless.@wifi-iface[0].user_enable=0 && uci set wireless.@wifi-iface[0].disabled=1 && uci commit && wifi
 +
 
 +
Let's overview what each segment indicates sequentially:
 +
* '''First line'''
 +
** 0 - the action must take place at minute 0 (right after the hour has changed)
 +
** 8 - the action must take place at 8 AM
 +
** * - all days of the month are applicable to the rule
 +
** * - all months of the year are applicable to the rule
 +
** 1-5 - the action must take place every Monday - Friday
 +
** uci set wireless.@wifi-iface[0].user_enable=1 - enables the WiFi AP
 +
** uci delete wireless.@wifi-iface[0].disabled - deletes the WiFi ''disabled'' option
 +
** uci commit - commits configuration changes
 +
** wifi - restarts the WiFi service
 +
* '''Second line'''
 +
** 0 - the action must take place at minute 0 (right after the hour has changed)
 +
** 18 - the action must take place at 6 PM
 +
** * - all days of the month are applicable to the rule
 +
** * - all months of the year are applicable to the rule
 +
** 1-5 - the action must take place every Monday - Friday
 +
** uci set wireless.@wifi-iface[0].user_enable=0 - disables the WiFi AP
 +
** uci set wireless.@wifi-iface[0].disabled=1 - disables the WiFi AP
 +
** uci commit - commits configuration changes
 +
** wifi - restarts the WiFi service
 +
* && - separates different commands
 +
 
 +
To sum up, the first line enables the WiFi AP at 8 AM, the second disables it at 6 PM. At the end of the enabling and disabling processes the changes are committed and the related services restarted. We used UCI to change the state of the WiFi AP. You can find more information on UCI in this article: '''[[UCI command usage]]'''.
   −
The first line enables the OpenVPN server at 8 AM, the second disables it at 6 PM. In this example we used UCI to change the state of the OpenVPN server. You find more information on UCI '''[[UCI command usage|here]]'''.
+
'''NOTE''': if you have multiple access points or had them in the past, the path to the relevant options (i.e., ''wireless.@wifi-iface[0]'') may be different. You check these paths with this command:
 +
 +
''uci show wireless | grep able''
    
==External links==
 
==External links==
   −
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html - PuTTY downloads page link
+
* https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html - PuTTY downloads page link
Anonymous user

Navigation menu