###
### /etc/systemd/system/mystartupscript.service
###
[Unit]
Description=My Startup Script
Wants=network-online.target
After=network.target network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/my_startup_script.sh
ExecStartPre=/bin/bash -c 'until host http://www.wttr.in; do sleep 1; done'
User=root
Group=root
[Install]
WantedBy=multi-user.target
###
### /usr/local/bin/my_startup_script.sh
###
#!/bin/bash
#error handler to exit with useful info
error() {
local message="$1"
local code="${2:-1}"
echo "Error: ${message}; exiting with status ${code}" 1>&2
exit "${code}"
}
#set up the variables - a directory to add to PATH, and a temporary output file
dirname='/opt/myapp/bin:'
outfile='/opt/myapp/weather.txt'
mkdir -p -- "$dirname" || error "Unable to create $dirname" 1;
touch "$outfile" || error "Failed to create $outfile" 1;
chmod 666 "$outfile"
#append directory to path
###avoiding spurious leading/trailing colon if $PATH is initially empty
###see https://unix.stackexchange.com/a/415028
##export PATH="${PATH:+${PATH}:}$dirname"
sed -e 's|'${dirname}'||g' -i /etc/environment
sed -e 's|PATH="\(.*\)"|PATH="'${dirname}'\1"|g' -i /etc/environment
#add timestamp to output file
date +%F:%T > "$outfile" || error "Unable to prepend timestamp to $outfile";
#append weather report to output file
curl -v 'http://wttr.in/Visby' >> "$outfile" || error "Unable to append weather report to $outfile";
Comments
0 B
|👍
/👎
0 B
|0 👍
/0 👎