mirror of https://github.com/alibaba/arthas.git
Arthas Async Jobs tutorial (#1354)
parent
86a45e9ce2
commit
e30eaae715
@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
|
||||
|
||||
下载`arthas-demo.jar`,再用`java -jar`命令启动:
|
||||
|
||||
`wget https://alibaba.github.io/arthas/arthas-demo.jar
|
||||
java -jar arthas-demo.jar`{{execute T1}}
|
||||
|
||||
`arthas-demo`是一个很简单的程序,它随机生成整数,再执行因式分解,把结果打印出来。如果生成的随机数是负数,则会打印提示信息。
|
@ -0,0 +1,2 @@
|
||||
|
||||
异步执行的命令,如果希望停止,可执行`kill`
|
@ -0,0 +1,6 @@
|
||||
|
||||
* When a job is executed in the background or in suspended status (use `ctrl + z` to suspend job), `fg <job-id>` can transfer the job to the foreground to continue to run.
|
||||
|
||||
* When a job is in suspended status (use `ctrl + z` to suspend job), `bg <job-id>` can put the job to the background to continue to run.
|
||||
|
||||
* A job created by other session can only be put to the foreground to run by using `fg` in the current session.
|
@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
|
||||
|
||||
Download `arthas-demo.jar` and start with the `java -jar` command:
|
||||
|
||||
`wget https://alibaba.github.io/arthas/arthas-demo.jar
|
||||
java -jar arthas-demo.jar`{{execute T1}}
|
||||
|
||||
`arthas-demo` is a very simple program that randomly generates integers, performs factorization, and prints the results.
|
||||
If the generated random number is negative, a error message will be printed.
|
@ -0,0 +1,2 @@
|
||||
|
||||
Asynchronous jobs in arthas. The idea is borrowed from [linux jobs](http://man7.org/linux/man-pages/man1/jobs.1p.html).
|
@ -0,0 +1,6 @@
|
||||
|
||||
Through this tutorial, you can know Arthas Async Jobs. More advanced features can be found in the Advanced Guide below.
|
||||
|
||||
* [Arthas Advanced](https://alibaba.github.io/arthas/arthas-tutorials?language=en&id=arthas-advanced)
|
||||
* [Arthas Github](https://github.com/alibaba/arthas)
|
||||
* [Arthas Documentation](https://alibaba.github.io/arthas/en)
|
@ -0,0 +1,63 @@
|
||||
{
|
||||
"title": "Arthas Async Jobs",
|
||||
"description": "Arthas Async Jobs",
|
||||
"difficulty": "expert",
|
||||
"time": "10 minutes",
|
||||
"details": {
|
||||
"steps": [
|
||||
{
|
||||
"title": "Arthas demo",
|
||||
"text": "arthas-demo.md"
|
||||
},
|
||||
{
|
||||
"title": "Start arthas-boot",
|
||||
"text": "arthas-boot.md"
|
||||
},
|
||||
{
|
||||
"title": "Async Jobs",
|
||||
"text": "async-jobs.md"
|
||||
},
|
||||
{
|
||||
"title": "Use & to run the command in the background",
|
||||
"text": "run-cmd-in-backgd.md"
|
||||
},
|
||||
{
|
||||
"title": "List background jobs",
|
||||
"text": "list-backgd-jobs.md"
|
||||
},
|
||||
{
|
||||
"title": "Suspend and cancel job",
|
||||
"text": "suspend-cancel-job.md"
|
||||
},
|
||||
{
|
||||
"title": "fg/bg, switch the job from the foreground to the background, and vise verse",
|
||||
"text": "switch-foregd-backgd.md"
|
||||
},
|
||||
{
|
||||
"title": "Redirect the output",
|
||||
"text": "redirect-output.md"
|
||||
},
|
||||
{
|
||||
"title": "Stop job",
|
||||
"text": "stop-job.md"
|
||||
},
|
||||
{
|
||||
"title": "Others",
|
||||
"text": "others.md"
|
||||
}
|
||||
],
|
||||
"intro": {
|
||||
"text": "intro.md"
|
||||
},
|
||||
"finish": {
|
||||
"text": "finish.md"
|
||||
}
|
||||
},
|
||||
"environment": {
|
||||
"uilayout": "terminal"
|
||||
},
|
||||
"backend": {
|
||||
"imageid": "java",
|
||||
"environmentsprotocol": "http"
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
|
||||
If you want to list all background jobs, you can execute the jobs command and the results are as follows:
|
||||
|
||||
`jobs`{{execute T2}}
|
||||
|
||||
```bash
|
||||
$ jobs
|
||||
[1]*
|
||||
Running trace demo.MathGame primeFactors &
|
||||
execution count : 49
|
||||
start time : Wed Jul 22 05:47:52 GMT 2020
|
||||
timeout date : Thu Jul 23 05:47:52 GMT 2020
|
||||
session : aa75753d-74f1-4929-a829-7ff965345183 (current)
|
||||
```
|
||||
|
||||
You can see that there is currently a background job executing:
|
||||
|
||||
job id is 10, `*` indicates that this job is created by the current session.
|
||||
|
||||
status is Stopped
|
||||
|
||||
execution count is the number of executions, which have been executed 19 times since the start.
|
||||
|
||||
timeout date: timeout timestamp, when the time exceeds this timestamp, the job will be automatically timeout and exit.
|
@ -0,0 +1,6 @@
|
||||
|
||||
* Support up to 8 commands at the same time to redirect the output to the log files.
|
||||
|
||||
* Do not open too many background jobs at the same time to avoid negative performance effect to the target JVM.
|
||||
|
||||
* If you do not want to stop the Arthas service and continue to perform background tasks, you can exit the Arthas console by executing `quit` command (`stop` command will stop the Arthas service)
|
@ -0,0 +1,20 @@
|
||||
|
||||
The job output can be redirect to the specified file by > or >>, and can be used together with &. By doing this, you can achieve running commands asynchronously, for example:
|
||||
|
||||
`trace demo.MathGame primeFactors >> test.out &`{{execute T2}}
|
||||
|
||||
The trace command will be running in the background and the output will be redirect to `~/logs/arthas-cache/test.out`. You can continue to execute other commands in the console, at the same time, you can also examine the execution result from the output file.
|
||||
|
||||
`cat test.out`{{execute T2}}
|
||||
|
||||
When connected to a remote Arthas server, you may not be able to view the output file on the remote machine. In this case, Arthas also supports automatically redirecting the output to the local cache file. Examples are as follows:
|
||||
|
||||
`trace demo.MathGame primeFactors >> &`{{execute T2}}
|
||||
|
||||
```bash
|
||||
$ trace Test t >> &
|
||||
job id : 2
|
||||
cache location : /Users/gehui/logs/arthas-cache/28198/2
|
||||
```
|
||||
|
||||
If output path is not given, Arthas will automatically redirect the output to the local cache. `Job id` and `cache location` will be shown on the console. `Cache location` is a directory where the output files are put. For one given job, the path of its output file contains `PID` and `job id` in order to avoid potential conflict with other jobs. In the above example, `pid` is 28198 and `job id` is 2.
|
@ -0,0 +1,6 @@
|
||||
|
||||
For example, execute the trace command in the background:
|
||||
|
||||
`trace demo.MathGame primeFactors &`{{execute T2}}
|
||||
|
||||
By doing this, the current command is put to the background to run, you can continue to execute other commands in the console.
|
@ -0,0 +1,2 @@
|
||||
|
||||
If you want to stop background job, just `kill <job-id>`.
|
@ -0,0 +1,8 @@
|
||||
|
||||
When the job is executing in the foreground, for example, directly executing the command` trace Test t`, or executing the background job command `trace Test t &`, then putting the job back to the foreground via fg command, the console cannot continue to execute other command, but can receive and process the following keyboard events:
|
||||
|
||||
`ctrl + z`: Suspends the job, the job status will change to Stopped, and the job can be restarted by `bg <job-id>` or `fg <job-id>`
|
||||
|
||||
`ctrl + c`: Stops the job
|
||||
|
||||
`ctrl + d`: According to linux semantics this should lead to exit the terminal, right now Arthas has not implemented this yet, therefore simply ignore this keystroke.
|
Loading…
Reference in New Issue