如何不将 nohup 输出附加到文件中而是替换它?

如何不将 nohup 输出附加到文件中而是替换它?

最近,我需要能够在后台运行脚本并监视另一台计算机的输出。由于这个原因,我能够使用这个命令:

$ nohup ./rsync.sh > ./output.txt

这是rsync.sh脚本包含的内容:

#!/bin/bash

set -ex

rsync -av --progress /home/martins/Documents/Copy/CopyFrom/ /home/martins/Documents/Copy/CopyTo/

echo "Done syncing!"

唯一的问题是我的output.txt文件不断增长,因为每次更新它附加输出到文件。

我想用它output.txt覆盖当前最新的我的脚本的输出。

否则我最终会得到这样的输出文件:

         32,768   0%   47.98kB/s   23:34:33  
      9,994,240   0%    9.49MB/s    0:06:57  
     34,897,920   0%   16.60MB/s    0:03:57  
     56,131,584   1%   17.80MB/s    0:03:40  
     75,792,384   1%   18.01MB/s    0:03:36  
    103,579,648   2%   22.25MB/s    0:02:54  
    125,337,600   3%   21.48MB/s    0:02:59  
    148,406,272   3%   21.91MB/s    0:02:54  
    170,164,224   4%   22.43MB/s    0:02:49  
    192,446,464   4%   21.10MB/s    0:02:59  
    213,090,304   5%   20.86MB/s    0:03:00  
    238,845,952   5%   21.51MB/s    0:02:53  
    264,273,920   6%   22.37MB/s    0:02:46  
    288,391,168   7%   22.78MB/s    0:02:42  
    311,721,984   7%   23.35MB/s    0:02:37  
    335,577,088   8%   22.89MB/s    0:02:39  
    360,480,768   8%   22.69MB/s    0:02:39  
    384,860,160   9%   22.79MB/s    0:02:37  
    412,745,728  10%   23.94MB/s    0:02:29  
    437,551,104  10%   24.17MB/s    0:02:26  
    466,386,944  11%   25.19MB/s    0:02:19  
    492,077,056  12%   25.47MB/s    0:02:17  
    514,883,584  12%   24.26MB/s    0:02:23  
    540,573,696  13%   24.43MB/s    0:02:21  
    566,263,808  13%   23.69MB/s    0:02:24  
    590,872,576  14%   23.48MB/s    0:02:24  
    616,595,456  15%   24.17MB/s    0:02:19  
    642,809,856  15%   24.27MB/s    0:02:17  
    668,762,112  16%   24.36MB/s    0:02:16  
    693,927,936  17%   24.42MB/s    0:02:15  
    719,355,904  17%   24.34MB/s    0:02:14  
    746,356,736  18%   24.59MB/s    0:02:12  
    776,044,544  19%   25.50MB/s    0:02:06  
    802,717,696  19%   25.91MB/s    0:02:03  
    825,524,224  20%   25.31MB/s    0:02:05  
    849,379,328  20%   24.48MB/s    0:02:08  
    875,855,872  21%   23.70MB/s    0:02:11  
    904,691,712  22%   24.21MB/s    0:02:07  
    931,954,688  22%   25.21MB/s    0:02:01  
    957,120,512  23%   25.56MB/s    0:01:59  
    984,973,312  24%   25.91MB/s    0:01:56  
  1,013,219,328  24%   25.63MB/s    0:01:56  
  1,044,676,608  25%   26.60MB/s    0:01:51  
  1,077,182,464  26%   28.34MB/s    0:01:43  
  1,104,969,728  27%   28.24MB/s    0:01:42  
  1,138,262,016  27%   29.54MB/s    0:01:36  
  1,163,067,392  28%   28.05MB/s    0:01:41  
  1,189,380,096  29%   26.64MB/s    0:01:45  
  1,211,924,480  29%   25.46MB/s    0:01:49  
  1,234,665,472  30%   23.00MB/s    0:02:00  
  1,260,683,264  30%   23.27MB/s    0:01:57  
  1,284,014,080  31%   22.47MB/s    0:02:01  
  1,312,325,632  32%   23.81MB/s    0:01:53  
  1,341,947,904  32%   25.39MB/s    0:01:44  
  1,368,948,736  33%   25.64MB/s    0:01:42  
  1,393,917,952  34%   26.12MB/s    0:01:40  
  1,418,756,096  34%   25.27MB/s    0:01:42  
  1,442,349,056  35%   23.87MB/s    0:01:47  
  1,468,563,456  36%   23.69MB/s    0:01:47  
  1,498,710,016  36%   24.93MB/s    0:01:40  
  1,530,429,440  37%   26.62MB/s    0:01:33  
  1,556,905,984  38%   27.30MB/s    0:01:29  
  1,587,314,688  38%   28.28MB/s    0:01:25  
  1,616,936,960  39%   28.09MB/s    0:01:25  
  1,643,413,504  40%   26.85MB/s    0:01:28  
  1,674,608,640  41%   27.96MB/s    0:01:23  
  1,707,343,872  41%   28.53MB/s    0:01:20  
  1,732,280,320  42%   27.47MB/s    0:01:23  
  1,758,756,864  43%   27.46MB/s    0:01:22  
  1,790,214,144  43%   27.47MB/s    0:01:21  
  1,811,709,952  44%   24.70MB/s    0:01:29  
  1,837,268,992  45%   24.86MB/s    0:01:27  
  1,863,090,176  45%   24.67MB/s    0:01:27  
  1,894,023,168  46%   24.59MB/s    0:01:26  
  1,922,072,576  47%   26.24MB/s    0:01:20  
  1,952,219,136  47%   27.28MB/s    0:01:15  
  1,979,023,360  48%   27.56MB/s    0:01:14  
  2,008,121,344  49%   27.18MB/s    0:01:14  
  2,037,678,080  50%   27.51MB/s    0:01:12  
  2,068,086,784  50%   27.62MB/s    0:01:10  
  2,096,136,192  51%   27.87MB/s    0:01:09  
  2,120,515,584  52%   26.66MB/s    0:01:11  
  2,147,811,328  52%   26.15MB/s    0:01:11  
  2,172,682,240  53%   24.80MB/s    0:01:14  
  2,200,403,968  54%   24.77MB/s    0:01:13  
  2,228,518,912  54%   25.72MB/s    0:01:09  
  2,257,616,896  55%   26.13MB/s    0:01:07  
  2,288,025,600  56%   27.47MB/s    0:01:03  
  2,316,599,296  56%   27.58MB/s    0:01:02  
  2,347,008,000  57%   28.10MB/s    0:00:59  
  2,375,843,840  58%   28.02MB/s    0:00:59  
  2,407,301,120  59%   28.28MB/s    0:00:57  
  2,436,136,960  59%   28.43MB/s    0:00:56  
  2,465,693,696  60%   28.28MB/s    0:00:55  
  2,492,235,776  61%   27.73MB/s    0:00:55  
  2,520,547,328  61%   26.99MB/s    0:00:56  
  2,547,286,016  62%   26.46MB/s    0:00:56  
  2,575,335,424  63%   26.09MB/s    0:00:56  
  2,603,909,120  63%   26.59MB/s    0:00:53  
  2,702,147,584  66%   43.25MB/s    0:00:30  
  3,271,327,744  80%  172.58MB/s    0:00:04  
  3,817,537,536  93%  296.24MB/s    0:00:00  
  4,071,903,232 100%   38.11MB/s    0:01:41

sent 4,492,670,113 bytes  received 209 bytes  39,237,295.39 bytes/sec
total size is 4,491,572,666  speedup is 1.00

+ echo 'Done syncing!'
Done syncing!

答案1

正如评论中所述,问题不在于nohup您提供的选项,而在于您提供的选项rsync。将脚本中的命令更改为

rsync -aq /home/martins/Documents/Copy/CopyFrom/ /home/martins/Documents/Copy/CopyTo/

并且您应该有一个更具可读性的日志文件。

相关内容