运行 使用 systemd 作为服务保存时,从代码中下载的文件在哪里

Where are downloaded files from code saved when run as a service using systemd

我有一个应用程序可以在无线更新期间从 s3 存储桶下载文件。当我直接从命令行 运行 代码时,我能够在应用程序目录中看到下载的文件,我的代码会选择这些文件来执行固件更新。但是当我 运行 代码作为服务时,即使调用成功,我也看不到下载的文件。我正在使用下面简化的代码来确定问题。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>

const char* url = "wget https://xxxxxxxx-xxxxxxx-xxxxxxxxxxxx-xx.s3.us-east 2.amazonaws.com/firmware.bin";

int main ()
{
 system((const char*)url);
}
  1. 运行 以上直接从命令行产生以下结果。可以在目录中看到firmware.bin

    debian@beaglebone:~/downloadTest$ sudo ./test 
    HTTP request sent, awaiting response... 200 OK
    Length: 131072 (128K) [application/octet-stream]
    Saving to: ‘firmware.bin’
    
    firmware.bin                                         100% 
    2019-12-08 20:36:03 (6.21 KB/s) - ‘firmware.bin’ saved [131072/131072]
    
    debian@beaglebone:~/downloadTest$ ls -l
    total 144
    -rw-r--r-- 1 root   root   131072 Dec  4 19:11 firmware.bin
    -rwxr-xr-x 1 debian debian   8384 Dec  8 20:12 test
    -rw-r--r-- 1 debian debian    354 Dec  8 20:00 test.c
    
  2. 现在当我运行将同一个程序作为服务时,我没有在目录中看到下载的文件。但是当我检查服务状态时,它显示如下,这意味着调用已通过并且文件确实已下载,事实上,我已经第 9 次尝试了。

    debian@beaglebone:~$ sudo systemctl status beaSmart.service 
    [sudo] password for debian: 
    ● beaSmart.service - BeaSmart Service
    Loaded: loaded (/etc/systemd/system/beaSmart.service; enabled; vendor preset: enabled)
    Active: inactive (dead) since Sun 2019-12-08 19:43:52 UTC; 59min ago
    Process: 3474 ExecStart=/home/debian/downloadTest/test (code=exited, status=0/SUCCESS)
    Main PID: 3474 (code=exited, status=0/SUCCESS)
    
    
    Dec 08 19:43:32 beaglebone test[3474]: HTTP request sent, awaiting response... 200 OK
    Dec 08 19:43:32 beaglebone test[3474]: Length: 131072 (128K) [application/octet-stream]
    Dec 08 19:43:32 beaglebone test[3474]: Saving to: ‘firmware.bin.9’
    Dec 08 19:43:41 beaglebone test[3474]:      0K .......... .......... .......... 
    .......... .......... 39% 5.33K 15s
    Dec 08 19:43:50 beaglebone test[3474]:     50K .......... .......... .......... 
    .......... .......... 78% 6.09K 5s
    Dec 08 19:43:52 beaglebone test[3474]:    100K .......... .......... ........                        
    100% 9.95K=20s
    Dec 08 19:43:52 beaglebone test[3474]: 2019-12-08 19:43:52 (6.27 KB/s) - 
    ‘firmware.bin.9’ saved [131072/131072]
    

关于文件 firmware.bin.9 保存在哪里的任何见解?。为什么它不保存在应用程序目录中?

默认情况下,wget 在当前工作目录中下载文件 运行。

A​​ systemd service working directory 可以用 WorkingDirectory= 指令设置。从link我们可以读到:

If not set, defaults to the root directory when systemd is running as a system instance and the respective user's home directory if run as user.

如果您的服务文件设置了默认的 WorkingDirectory 并且 运行 作为系统实例,则该文件很可能最终位于根目录中。