Hello, On Fri, 18 Dec 2020, Marco Calistri wrote:
(Note: This is a generic Linux/Unix question not specifically related to Opensuse)
Here a weird fact I face running a cron job from /etc/cron.hourly which it looks to trim away the final part of the text I aim to get.
Below is the signature generated by script I add into the cron job:
--
Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel: 5.9.14-1-default Desktop:
Whereas here at the bottom the same script result if I run it from my console:
-- Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel: 5.9.14-1-default Desktop: (Xfce 4.14) <----------------As you can see the script ran by the cron job it is missing this part.
I suppose it could be caused by some variables settings which are different from my user environment and the ones used by the cron user.
This is the script I use:
#!/bin/sh echo "Marco Calistri" > /home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt echo "Build: $(head -n 1 /etc/issue|awk -F'to' '{print $2}'|awk -F'-' '{print $1}')" >> /home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt echo "Kernel: $(uname -r)" >> /home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt echo "Desktop:$(xfce4-session --version |awk -F'xfce4-session' '{print $2}'|awk -F'2' '{print $2}')" >> /home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt echo -ne "\n" >> /home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt
First of all: your /bin/sh is probably bash anyway, so use it. But IIRC the following is POSIX compatible as well. a) you should use printf instead of echo b) if all you do is append stuff to a file you have 3 ways: 1) use a variable for the filename #!/bin/bash OSRF="/home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt" printf "...\n" >"$OSRF" printf "...\n" >>"$OSRF" .... 2) use a block #!/bin/bash OSRF="/home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt" { printf "...\n" printf "...\n" .... } >"$OSRF" 3) use exec OSRF="/home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt" >"$OSRF" # exec 1>>"$OSRF" printf "...\n" printf "...\n" .... c) there's nothing you can't do with just one awk that you do with two d) for your problem running an X program from cron there's a nice workaround: rpm -q --queryformat 'Desktop: XFCE (%{version})\n' \ -f /usr/bin/xfce4-session (short form of '--queryformat' is '--qf' (with '--'), '-qf' is '-q -f' and something quite different (and also used here ;) In summary: ==== #!/bin/bash OSRF="/home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt"
"$OSRF" ### clear file exec 1>>"$OSRF"
printf 'Marco Calistri\n' rpm -q --qf 'Build: openSUSE Tumbleweed %{version}\n' -f /etc/os-release printf 'Kernel: %s\n' "$(uname -r)" rpm -q --qf 'Desktop: XFCE (%{version})\n' -f /usr/bin/xfce4-session ==== For the "Build" line, you could also parse os-release: awk -F'[="]' '/PRETTY/{pn=$3;}/VERSION_ID/{v=$3;} END { printf("Build: %s %s\n", pn, v); }' /etc/os-release or eval /etc/os-release for the "Build" line (UNSAFE): eval "$(</etc/os-release)" printf 'Build: %s %s\n' "$PRETTY_NAME" "$VERSION_ID" None of this is in any way dependent on the env, so should run with or without X and XFCE even running. HTH, -dnh -- Zack Allan: "It just doesn't make any sense." Sheridan: "If you're gonna wait for the universe to start making sense you'll have a *long* wait ahead of you." -- Babylon 5 - 4x12 - Conflicts of Interest