在终端中运行一个无限循环,每隔 2 秒钟打印一行信息”hello world”输出添加到 file2.txt 尾部,使用 ps 命令(不使用任何参数)查找前述无限循环进程的进程号,使用 kill命令结束这个进程
时间: 2023-05-27 11:05:46 浏览: 61
1. 在终端中输入以下命令,运行一个无限循环并将输出添加到 file2.txt 尾部:
```
while true; do echo "hello world" >> file2.txt; sleep 2; done
```
2. 打开另一个终端窗口,输入以下命令,查找该进程的进程号:
```
ps ax | grep 'while true; do echo "hello world" >> file2.txt; sleep 2; done'
```
3. 在输出中找到该进程的进程号,并记下来。
4. 返回原来的终端窗口,在其中输入以下命令,使用刚才查找到的进程号结束该进程:
```
kill <进程号>
```
其中,<进程号> 替换成刚才查找到的进程号。
相关问题
在终端中运行一个无限循环,每隔 2 秒钟打印一行信息”hello world”输出添加到 file2.txt 尾部
可以使用以下命令:
```shell
while true; do echo "hello world" >> file2.txt; sleep 2; done
```
解释:
- `while true; do` 表示无限循环。
- `echo "hello world" >> file2.txt;` 表示每次循环输出一行信息到 file2.txt 文件的末尾。
- `sleep 2;` 表示每次循环暂停 2 秒钟。
- `done` 表示循环结束。
这样就可以在终端中运行一个无限循环,并将输出添加到 file2.txt 文件的末尾。
在终端中运行一个无限循环,每隔 2 秒钟打印一行信息”hello world”输出添加到 file2.txt 尾部,并且在屏幕上输出
以下是在终端中运行无限循环并将输出添加到 file2.txt 尾部的命令:
```
while true; do echo "hello world" | tee -a file2.txt; sleep 2; done
```
其中,`while true` 表示无限循环,`echo "hello world"` 表示输出 "hello world",`tee -a file2.txt` 表示将输出同时打印到屏幕上并添加到 file2.txt 尾部,`sleep 2` 表示暂停 2 秒钟。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)