cron job signatures script

Hello, (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 Thanks for any eventual suggestion you could provide to me. Regards, Marco

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday, 2020-12-18 at 13:19 -0300, Marco Calistri wrote:
When I run the last but one line in my terminal I get: cer@Telcontar:~> echo "Desktop:$(xfce4-session --version |awk -F'xfce4-session' '{print $2}'|awk -F'2' '{print $2}')" Desktop:.1 (Xfce 4.1 cer@Telcontar:~> which is not what you say you get. I tried also as root, as your cronjob runs as root: Telcontar:~ # echo "Desktop:$(xfce4-session --version |awk -F'xfce4-session' '{print $2}'|awk -F'2' '{print $2}')" Desktop:.1 (Xfce 4.1 Telcontar:~ # I I run tests to try to see what sections of that line produce, they fail on me. As I can not read awk, I can't do much. But I can produce the same result. cer@Telcontar:~> xfce4-session --version | grep xfce4-session | cut -d" " -f3,4 (Xfce 4.12) cer@Telcontar:~> echo -n "Desktop: " >> file echo "xfce4-session --version | grep xfce4-session | cut -d" " -f3,4" >> file or VERSION=`xfce4-session --version | grep xfce4-session | cut -d" " -f3,4"` echo "Desktop: $VERSION" >> file or VERSION=`xfce4-session --version | head -1 | cut -d" " -f3,4` echo "Desktop: $VERSION" >> file - -- Cheers, Carlos E. R. (from openSUSE 15.1 x86_64 at Telcontar) -----BEGIN PGP SIGNATURE----- iHoEARECADoWIQQZEb51mJKK1KpcU/W1MxgcbY1H1QUCX90CVhwccm9iaW4ubGlz dGFzQHRlbGVmb25pY2EubmV0AAoJELUzGBxtjUfVUWgAnja5O+Yp7OI88LU9YTjG bmELnhU9AKCOpv47+2U4E/O1JaocLyYsh6rl6g== =sxFb -----END PGP SIGNATURE-----

Il 18/12/20 16:40, Brian Reichert ha scritto:
I've not understood what you mean with "new non-login shell". Have I to found out a system user which has not a login shell? Or it is sufficient I simply run such command sequence on my own user shell? But the point is how to force cron to not trim out a part of the text I expect to write in the text file. Sorry and thanks for your response. -- Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel: 5.9.14-1-default Desktop:

On 18/12/2020 23.21, Marco Calistri wrote:
You could use /bin/bash. Why sh? Do the change in your script, too.
I've not understood what you mean with "new non-login shell".
Google "what is a login shell in linux?" A login shell is a shell given to a user upon login into their user account. ... The general cases for having a login shell include: Accessing your computer remotely using ssh. Simulating an initial login shell with bash -l or sh -l. Simulating an initial root login shell with sudo -i. help.gnome.org › users › stable › pref-login-shell.html.en Login shells <https://help.gnome.org/users/gnome-terminal/stable/pref-login-shell.html.en> Google "what is a non-login shell in linux?" When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. unix.stackexchange.com › questions › difference-between.. Difference between Login Shell and Non-Login Shell? - Unix ... <https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell>
Precisely. The idea is to run the script in the same environment cron will use, to find out what the problem is. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)

Il 18/12/20 19:46, Carlos E. R. ha scritto:
And regarding all the googling directions I say thanks but I had no time nor will to read it today. I've tried to replace "on the fly" the xfce version string of my original script using your which uses cut and it's look not good as per new signature below. Cheers, -- Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel:5.9.14-1-default Desktop:aprire il --help' per

On 19/12/2020 00.19, Marco Calistri wrote:
Well, there must be a different "env" which affects it. I would play would the script triggered from a cron every minute till I got it right. It is just a debug and repeat problem. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)

Il 18/12/20 23:13, Carlos E. R. ha scritto:
...Really boring troubleshooting: a system executed script that want to "play differently" of the same piece of strings played by a normal user... :-/ 8-) Oh my God! Look below! Something has changed at the end :-) -- Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel:5.9.14-1-default (Xfce 4.14) Desktop: (Xfce 4.14)

On 19/12/2020 15.29, Marco Calistri wrote:
BUt cron jobs DO play differently than the same thing done as a user. It is a characteristic, it is perfectly known. We told you, the environment is different.
8-) Oh my God! Look below! Something has changed at the end :-)
-- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)

On Fri, Dec 18, 2020 at 07:21:24PM -0300, Marco Calistri wrote:
What I meant was that your shell, when you are running that script, is a login shell, and when you've logged in all sorts of magic has fired to set up your environment, configure shell macros, alter your PATH, etc. Look at the man pagwefor bash, under the section named 'INVOCATION'. (Now that I'm reading it myself, 'interactive' also fires extra logic.) The point is, a shell script invoked by cron has a shell with a different configuration that you having interavively logged into your host.
-- Brian Reichert <reichert@numachi.com> BSD admin/developer at large

On Fri, Dec 18, 2020 at 07:27:08PM -0300, Marco Calistri wrote:
I did not retain a copy of your original script. Clearly, though, your script relies on some pre-established aspect of your environment. Cron's default path is, I think, '/usr/bin:/bin'. You can test-fire a script with a similar environment, e.g.: $ env - PATH=/usr/bin:/bin /usr/bin/printenv PATH=/usr/bin:/bin So, instead try your script like this: env - PATH=/usr/bin:/bin /bin/sh /etc/cron.hourly/sig-os-release
-- Brian Reichert <reichert@numachi.com> BSD admin/developer at large

Hello, On Fri, 18 Dec 2020, Marco Calistri wrote:
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

Hello, On Sat, 19 Dec 2020, Marco Calistri wrote:
'exec' is a shell-builtin, and the cleaning is done the line above by the '>"$OSRF"' (which outputs "nothing" into the file).
You got a missing " somewhere. And mind you, you should not use printf as you use echo!
#!/bin/sh OSRF="/home/marco/.thunderbird/cy71896h.Marco/signatures/os-release.txt"
OK.
#>>"$OSRF"
That'd be appending, not "nulling" the output file, use >"$OSRF" (just one '>' in front of the filename (variable).
printf "Marco Calistri\n" >> "$OSRF"
ok.
printf "Build:$(head -n 1 /etc/issue|awk -F'to' '{print $2}'|awk -F'-' '{print $1}')" >> "$OSRF"
If you must: printf 'Build: %s\n' \ "$(head -n 1 /etc/issue | awk -F'to' '{print $2}' | \ awk -F'-' '{print $1}')" But see my version with rpm/parsing of os-release!
printf "Kernel:$(uname -r)" >> "$OSRF"
ok.
Just use the script I proposed! ==== #!/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 ==== It does work! No matter if XFCE is or is not running! *hrmpf*, -dnh --
Tell me again why you would want to emulate Paris Hilton? -- jeff shultz To be blond, rich, and have a nice rack? -- dima

Hello, On Sat, 19 Dec 2020, Carlos E. R. wrote:
I know. Marco sent to 'opensuse-support@opensuse.org' and my mutt honored that while also sending to the configured list support@lists.o.o. Happens only occasionally. And I don't always catch it before sending. Sorry about that. -dnh -- Outside Context Problem (OCP), the kind of problem "most civilisations would encounter just once, and which they tended to encounter rather in the same way a sentence encountered a full stop." -- Iain M. Banks, in a Culture novel

On 19/12/2020 22.33, Marco Calistri wrote:
No, that would be disastrous. Millions of people have the old address saved, you would piss them a lot if that address would bounce. We try to cause minimal disturbation and damage. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday, 2020-12-18 at 13:19 -0300, Marco Calistri wrote:
When I run the last but one line in my terminal I get: cer@Telcontar:~> echo "Desktop:$(xfce4-session --version |awk -F'xfce4-session' '{print $2}'|awk -F'2' '{print $2}')" Desktop:.1 (Xfce 4.1 cer@Telcontar:~> which is not what you say you get. I tried also as root, as your cronjob runs as root: Telcontar:~ # echo "Desktop:$(xfce4-session --version |awk -F'xfce4-session' '{print $2}'|awk -F'2' '{print $2}')" Desktop:.1 (Xfce 4.1 Telcontar:~ # I I run tests to try to see what sections of that line produce, they fail on me. As I can not read awk, I can't do much. But I can produce the same result. cer@Telcontar:~> xfce4-session --version | grep xfce4-session | cut -d" " -f3,4 (Xfce 4.12) cer@Telcontar:~> echo -n "Desktop: " >> file echo "xfce4-session --version | grep xfce4-session | cut -d" " -f3,4" >> file or VERSION=`xfce4-session --version | grep xfce4-session | cut -d" " -f3,4"` echo "Desktop: $VERSION" >> file or VERSION=`xfce4-session --version | head -1 | cut -d" " -f3,4` echo "Desktop: $VERSION" >> file - -- Cheers, Carlos E. R. (from openSUSE 15.1 x86_64 at Telcontar) -----BEGIN PGP SIGNATURE----- iHoEARECADoWIQQZEb51mJKK1KpcU/W1MxgcbY1H1QUCX90CVhwccm9iaW4ubGlz dGFzQHRlbGVmb25pY2EubmV0AAoJELUzGBxtjUfVUWgAnja5O+Yp7OI88LU9YTjG bmELnhU9AKCOpv47+2U4E/O1JaocLyYsh6rl6g== =sxFb -----END PGP SIGNATURE-----

Il 18/12/20 16:40, Brian Reichert ha scritto:
I've not understood what you mean with "new non-login shell". Have I to found out a system user which has not a login shell? Or it is sufficient I simply run such command sequence on my own user shell? But the point is how to force cron to not trim out a part of the text I expect to write in the text file. Sorry and thanks for your response. -- Marco Calistri Build: openSUSE Tumbleweed 20201216 Kernel: 5.9.14-1-default Desktop:

On 18/12/2020 23.21, Marco Calistri wrote:
You could use /bin/bash. Why sh? Do the change in your script, too.
I've not understood what you mean with "new non-login shell".
Google "what is a login shell in linux?" A login shell is a shell given to a user upon login into their user account. ... The general cases for having a login shell include: Accessing your computer remotely using ssh. Simulating an initial login shell with bash -l or sh -l. Simulating an initial root login shell with sudo -i. help.gnome.org › users › stable › pref-login-shell.html.en Login shells <https://help.gnome.org/users/gnome-terminal/stable/pref-login-shell.html.en> Google "what is a non-login shell in linux?" When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. unix.stackexchange.com › questions › difference-between.. Difference between Login Shell and Non-Login Shell? - Unix ... <https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell>
Precisely. The idea is to run the script in the same environment cron will use, to find out what the problem is. -- Cheers / Saludos, Carlos E. R. (from 15.1 x86_64 at Telcontar)
participants (5)
-
Brian Reichert
-
Carlos E. R.
-
Carlos E.R.
-
David Haller
-
Marco Calistri