-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschedualing.Rmd
257 lines (171 loc) · 5.68 KB
/
schedualing.Rmd
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
---
title: "Schedualing"
author: "`r Sys.Date()`<br><p style='color:royalblue'>[Mansun Kuo](https://tw.linkedin.com/pub/mansun-kuo/82/3b4/344)</p>"
date: '`r Sys.Date()`'
#ratio: 4x3
ratio: 16x10
output:
rmdshower::shower:
self_contained: true
katex: false
theme: material
css: css/shower.css
params:
refresh: no
---
```{r include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Schedualing {.white .Section}
<img src="img/bali.jpg" class="cover">
# Task Schedualing in R
Sometime you may need to execute your R script periodically and automatically:
- **Regular Report**: Generate monthly/daily report automatically
- **Crawler**: Get latest data every hour
- **Forecasting System**: Rebuild your preditive model with latest data
# Steps to Schedualing
1. Invoking a batch job of R
- RScript
- R CMD BATCH
2. Passing arguments to R
3. A schedualer:
- Windows: Task Schedualer
- Linux/Mac: Crontab
# Open shell with RStudio
Environment variable of R and RScript will be exported in this way.
<img src="img/shell.png" width="800">
# Invoke a batch job
- **Rscript**
- A binary front-end to execute a R script
- Recommended
- Execute a R script: Just like other programming language, you can pass arguments to your R script
```bash
Rscript <PATH OF YOUR R SCRIPT> <ARG1> <ARG2> ...
```
- **R CMD BATCH**
- Run R in batch mode
- I never use this one since I use Rscript
# Shebang
If you use Linux or Mac, you can execute a R script directly with
[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))
and permission of execution.
```r
#!/usr/bin/env Rscript
```
Add permission of execution:
```{r engine='bash', echo=TRUE}
chmod u+x schedualing/now.R
```
Check permission:
```{r engine='bash', echo=TRUE}
ls -l schedualing/now.R
```
# Passing arguments
- Function:
- **commandArgs**: a function to access command line arguments
- Package:
- [argparser](https://cran.r-project.org/web/packages/argparser/index.html): Command-line argument parser written purely in R
- [ArgParser](https://github.com/everdark/ArgParser): a command line argument parser for R from Kyle
- [argparse](https://cran.r-project.org/web/packages/argparse/index.html): Analog to Python's argparse
- [optparse](https://cran.r-project.org/web/packages/optparse/index.html): Analog to Python's optparse
In most situation, I will use a package to dealing with arguments parsing.
# Exercise
Please download [our github repository](https://github.com/mansunkuo/BeyondBasicR) under *github* directory.
Let's try to pass some arguments to [schedualing/now.R](schedualing/now.R):
Get help:
```bash
Rscript schedualing/now.R -h
```
Pass a positional argument:
```bash
Rscript schedualing/now.R Mansun
```
Add a flag *-c*:
```bash
Rscript schedualing/now.R Mansun -c
```
Add a optional argument:
```bash
Rscript schedualing/now.R Mansun -c -g "How are you?"
```
# now.R
```{r results='asis', echo=FALSE}
catfile = "schedualing/now.R"
cat("```r",
readChar(catfile, nchars = file.info(catfile)$size),
"```",
sep = "\n"
)
```
# Task Schedualer(Windows)
Launch task scheduler on Windows:
1. Press **Windows Logo+R** to run dialog box
2. Enter *control schedtasks*
Launch task schedualer within RStudio:
```r
# Execute a system conmmand to launch task schedualer
system("control schedtasks")
```
# Set trigger
<img src="img/trigger.png" width="100%">
# Set job
<img src="img/taskscheduler.png" width="100%">
# Crontab(Linux/Mac)
Maintain crontab files to execute scheduled commands in Unix-like OS for individual users.
```
crontab [-u user] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
```
You may need to use some command line editor like vim
when using *crontab -e* to edit your crontab.
Type *select-editor* in terminal to choose your favorite editor
# Your first crontab
- Download [our github repository](https://github.com/mansunkuo/BeyondBasicR) under *github* directory
- Make sure your library *argparser* are installed systemwide.
- Open [crontab.txt](schedualing/crontab.txt). Edit it as your full path and save it.
Always change directory to your working directory to prevent unexpected error.
```bash
# Execute every minutes
* * * * * cd /home/mansun/github/BeyondBasicR/schedualing; ./now.R Mansun
```
- Open your terminal and enter following command
```
crontab schedualing/crontab.txt
```
- Type *crontab -l* to check your crontab:
```{r engine='bash'}
crontab -l
```
# Configure your cron job
```
┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday)
│ │ │ │ │
* * * * * command to execute
```
- Comments begin with a comment mark **#**
- Separate items of a list with **Comma(,)**.
```
# Execute at 00:00 and 12:00 everyday
0 0,12 * * * command to execute
```
- Define ranges with **Hyphen(\-)**
```
# Execute at 06:00 every Monday to Friday
0 6 * * 1-5 command to execute
```
Please refer to
[Cron](https://en.wikipedia.org/wiki/Cron)
for further information.
# Exercise
Set a schedualing job on your favorite OS.
# References
- [cron](https://en.wikipedia.org/wiki/Cron)
- [Scheduling R Tasks via Windows Task Scheduler](https://trinkerrstuff.wordpress.com/2015/02/11/scheduling-r-tasks-via-windows-task-scheduler/)