0

I am currently trying to set up a bash script that records the ipv6 address of a host machine (raspberry pi running buster) and writes it to an env file the script creates. The script runs fine if I execute manually, but it doesn't seem to execute successfully on boot when enabled; I don't see the env file it is supposed to create in the appropriate directory.

I wonder if this is a permissions issue in regard to creating the .env file? Hoping someone might be able to shed light on how to trouble shoot this? I have set the following permissions on files as well as tried feed -c type to the exec start option.

OS and hardware:

OS: buster lite
host: raspberry pi 4

Permissions and prep:

# copy file to the bin folder and add permissions
cp /home/pi/my_project/ip_addr/linux_get_ip.sh /usr/local/bin/linux_get_ip.sh
sudo chmod 744 /usr/local/bin/linux_get_ip.sh
sudo chmod 644 /etc/systemd/system/get_ip.service

Bash script: linux_get_ip.sh

#!/bin/bash

# test existence of a directory
cd ./my_project/ip_addr/

# create an env file
touch .env

# assign the ip address to that env file
MACHINE_HOST_IP="$(hostname -I | cut -d " " -f 2)"
echo "${MACHINE_HOST_IP}"
echo "$(ls)"
destdir=.env
echo "MACHINE_HOST_IP=$MACHINE_HOST_IP" > "$destdir"

Service: get_ip.service

[Unit]
 Description=Get IP address of local machine
 After=multi-user.target

 [Service]
 Type=idle
 ExecStart=/bin/bash -c /usr/local/bin/linux_get_ip.sh

 [Install]
 WantedBy=multi-user.target

systemctl status get_ip.service

● get_ip.service - Get IP address of local machine
     Loaded: loaded (/etc/systemd/system/get_ip.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2022-04-04 12:26:31 EDT; 8min ago
    Process: 1235 ExecStart=/bin/bash -c /usr/local/bin/linux_get_ip.sh (code=exited, status=0/SUCCESS)
   Main PID: 1235 (code=exited, status=0/SUCCESS)
        CPU: 51ms

Apr 04 12:26:31 raspberrypi bash[1235]: proc
Apr 04 12:26:31 raspberrypi bash[1235]: root
Apr 04 12:26:31 raspberrypi bash[1235]: run
Apr 04 12:26:31 raspberrypi bash[1235]: sbin
Apr 04 12:26:31 raspberrypi bash[1235]: srv
Apr 04 12:26:31 raspberrypi bash[1235]: sys
Apr 04 12:26:31 raspberrypi bash[1235]: tmp
Apr 04 12:26:31 raspberrypi bash[1235]: usr
Apr 04 12:26:31 raspberrypi bash[1235]: var
Apr 04 12:26:31 raspberrypi systemd[1]: get_ip.service: Succeeded.
Ken White
  • 123,280
  • 14
  • 225
  • 444
LoF10
  • 1,907
  • 1
  • 23
  • 64
  • 2
    You probably want to use an absolute path instead of `./` in your script. Currently your script seems to write the `.env` file to `/`, judging from the output of `ls` in your systemd log – mashuptwice Apr 04 '22 at 17:09
  • 1
    This question is more suitable for [unix.se] instead, as it has nothing to do with programming. This site is strictly for questions related to programming (code) or use of a programmer's tool (IDE, compiler, etc.). You can find more information about this site in the [help]. – Ken White Apr 04 '22 at 17:26

0 Answers0