[opensuse] Different behaviour of bash script when run from cron
I have a bash script to generate the sig featured below. #!/bin/bash # script to generate a topical linux mailsig SIGFILE=/home/bob/Documents/Signatures/daily.sig : > $SIGFILE # (re)creates an empty SIGFILE echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)" kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt` plasma=`kcmshell5 -v | awk '{print $2}'` echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0 When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads: Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: The error message returned by the system is: QXcbConnection: Could not connect to display I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference. Can anyone suggest a solution/workaround, please? -- Bob Williams System: Linux 4.4.22-1-default Distro: openSUSE 42.2 Beta (x86_64) Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: 5.8.0
On Mon, 17 Oct 2016, Bob Williams wrote:
I have a bash script to generate the sig featured below. When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version.
You didn't show us the line in crontab. Does crontab execute the script as root? Can root get the plasma version? Roger -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 17/10/16 17:58, Roger Price wrote:
On Mon, 17 Oct 2016, Bob Williams wrote:
I have a bash script to generate the sig featured below. When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version.
You didn't show us the line in crontab. Does crontab execute the script as root? Can root get the plasma version?
Roger
Sorry. Here it is. 0 7 * * * bash /home/bob/bin/daily.sh That's from my user crontab. I'll try putting it in the system crontab. -- Bob Williams System: Linux 4.4.22-1-default Distro: openSUSE 42.2 Beta (x86_64) Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: 5.8.0
On Monday, 17 October 2016 20:50:23 BST Bob Williams wrote:
On 17/10/16 17:58, Roger Price wrote:
On Mon, 17 Oct 2016, Bob Williams wrote:
I have a bash script to generate the sig featured below. When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version.
You didn't show us the line in crontab. Does crontab execute the script as root? Can root get the plasma version?
Roger
Sorry. Here it is.
0 7 * * * bash /home/bob/bin/daily.sh
That's from my user crontab. I'll try putting it in the system crontab.
I put a similar signature script into the plasma startup on login rather than use cron -- opensuse:tumbleweed:20161013 Qt: 5.7.0 KDE Frameworks: 5.27.0 KDE Plasma: 5.8.0 kwin5-5.8.0-156.1.x86_64 kmail5-16.08.1-1.3.x86_64 Kernel: 4.7.6-1-default Nouveau: 1.0.13_1.1 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
sorry, test -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 18/10/16 08:57, ianseeks wrote:
On Monday, 17 October 2016 20:50:23 BST Bob Williams wrote:
On 17/10/16 17:58, Roger Price wrote:
On Mon, 17 Oct 2016, Bob Williams wrote:
I have a bash script to generate the sig featured below. When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version.
You didn't show us the line in crontab. Does crontab execute the script as root? Can root get the plasma version?
Roger
Sorry. Here it is.
0 7 * * * bash /home/bob/bin/daily.sh
That's from my user crontab. I'll try putting it in the system crontab.
I put a similar signature script into the plasma startup on login rather than use cron
Yes, I think that's what I'm going to do. Though I can report that adding the line export DISPLAY=:0.0 to my script did make it work in the cron -- Bob Williams System: Linux 4.4.22-1-default Distro: openSUSE 42.2 Beta (x86_64) Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: 5.8.0
Bob -- ...and then Bob Williams said... % % Yes, I think that's what I'm going to do. Though I can report that % adding the line % % export DISPLAY=:0.0 % % to my script did make it work in the cron Hmmm... And did you try that when not logged in or when logged in as some other user? If "bob" owns the display, then when "bob" runs a cron job specifying that display it will probably work, but when, say, "root" owns the display then "bob" will not be allowed to connect. Better to go with a .profile or a plasma/kde startup script to cache your desired info. But this is good experience for you :-) HTH & HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On October 17, 2016 8:33:05 AM PDT, Bob Williams <linux@karmasailing.uk> wrote:
I have a bash script to generate the sig featured below.
#!/bin/bash
# script to generate a topical linux mailsig
SIGFILE=/home/bob/Documents/Signatures/daily.sig
: > $SIGFILE # (re)creates an empty SIGFILE
echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)" kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt` plasma=`kcmshell5 -v | awk '{print $2}'` echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
Cache the plasma version in a file somewhere. Realistically, how often does it change? You could ask zypper I suppose, but it seems like a lot of trouble for something nobody reads. Ever. -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 17/10/16 18:03, John Andersen wrote:
On October 17, 2016 8:33:05 AM PDT, Bob Williams <linux@karmasailing.uk> wrote:
I have a bash script to generate the sig featured below.
#!/bin/bash
# script to generate a topical linux mailsig
SIGFILE=/home/bob/Documents/Signatures/daily.sig
: > $SIGFILE # (re)creates an empty SIGFILE
echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)" kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt` plasma=`kcmshell5 -v | awk '{print $2}'` echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
Cache the plasma version in a file somewhere. Realistically, how often does it change? You could ask zypper I suppose, but it seems like a lot of trouble for something nobody reads. Ever.
Thank you. I'm not really bothered if anyone reads it or not. My question was to try and understand why something works from the command line but not when run as a cronjob. Maybe I shouldn't have asked for a workaraound. -- Bob Williams System: Linux 4.4.22-1-default Distro: openSUSE 42.2 Beta (x86_64) Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: 5.8.0
Bob -- ...and then Bob Williams said... % ... % try and understand why something works from the command line but not Good for you! That's excellent, and I hope that my answer was helpful. Yamaban went even farther (although I'll be surprised if setting $DISPLAY when it can't get to it will work; please report -- and be sure to test when you aren't logged in or when some other account is using the windowing environment, too), so you should now have a pretty good idea of where to research more. Have fun :-) % when run as a cronjob. Maybe I shouldn't have asked for a workaraound. Well, you ask and you'll get ... ;-) HTH & HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 10/17/2016 10:17 AM, Bob Williams wrote:
My question was to try and understand why something works from the command line but not when run as a cronjob.
I thought you pretty much answered that when asking the question. Cronjobs just get a shell, and it does not have access to your graphical environment. -- After all is said and done, more is said than done.
17.10.2016 18:33, Bob Williams пишет:
I have a bash script to generate the sig featured below.
#!/bin/bash
# script to generate a topical linux mailsig
SIGFILE=/home/bob/Documents/Signatures/daily.sig
: > $SIGFILE # (re)creates an empty SIGFILE
echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)" kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt` plasma=`kcmshell5 -v | awk '{print $2}'` echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
How do you expect it to get version of graphical environment if there is no graphical environment to start with?
Can anyone suggest a solution/workaround, please?
Do not use cron. If your script depends on presence of DE, run it when you are logged in DE. It's not as this information changes so often, so running it once on login would be enough. Otherwise you could of course export DISPLAY in cron command, but you may not even be logged in when cron task is executed.
Bob -- ...and then Bob Williams said... % % I have a bash script to generate the sig featured below. ... % plasma=`kcmshell5 -v | awk '{print $2}'` ... % % When run from my user command line it works correctly, but when I run it % from my crontab, it fails to get the plasma version. So the last line of ... % QXcbConnection: Could not connect to display % % I'm guessing that cron is not running in graphical environment, but not % sure why that would make a difference. Because kcmshell5 is part of KDE is a graphic environment. You wouldn't be able to run xterm without a $DISPLAY, either. % % Can anyone suggest a solution/workaround, please? You *might* be able to fake it by passing -display :0 on the command line, but almost certainly only if you're logged in at the time if it will work at all. You could try looking for a command-line arg that says "just dump your info and don't care about a display", or you could install a null-display that tools like this could use. You could also run strings on the binary and look for the info, since it's probably in there as plain text. Or you could use a Plasma/kcmshell version file (eg $HOME/.kcmshell5-v or such) to build your .sig and then update that file via your .profile (if you have $DISPLAY set, of course) to keep it current. For that matter, though, how often does your desktop config change? I'd think that that one line is pretty constant and that you could update it only every blue moon as part of your "let's install something new" procedure. YMMV :-) HTH & HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Mon, 17 Oct 2016 17:33, Bob Williams wrote:
I have a bash script to generate the sig featured below.
#!/bin/bash
# script to generate a topical linux mailsig
SIGFILE=/home/bob/Documents/Signatures/daily.sig
: > $SIGFILE # (re)creates an empty SIGFILE
echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)"
May or may not work:
kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt`
And here comes the blopper, that will not work for sure:
plasma=`kcmshell5 -v | awk '{print $2}'`
echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
TL;DR: Missing DISPLAY variable in enviornment from cron. What happens in detail: - a script from cron runs in a minimal environment. - only very few environment variables are defined at all. The command "kf5-config" wants / partially needs a working "DISPLAY". The command "kcmshell5" needs a set "DISPLAY" variable or it fails. Try with a extra line before / after the SIGFILE=.... line: [code] export DISPLAY=:0.0 [/code] Other variables may be needed also. try to start your script from an other script via a line: #/usr/bin/bash exec -c <your-sig-script> to simulate a empty environment. also see "help exec" for details on the "-c" option. - Yamaban. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 17/10/16 18:30, Yamaban wrote:
On Mon, 17 Oct 2016 17:33, Bob Williams wrote:
I have a bash script to generate the sig featured below.
#!/bin/bash
# script to generate a topical linux mailsig
SIGFILE=/home/bob/Documents/Signatures/daily.sig
: > $SIGFILE # (re)creates an empty SIGFILE
echo "Bob Williams" >> $SIGFILE echo " System: " `uname -sr` >> $SIGFILE distro=" $(head -n 1 /etc/SuSE-release)"
May or may not work:
kde=`kf5-config -v | grep KDE` Qt=`kf5-config -v | grep Qt`
And here comes the blopper, that will not work for sure:
plasma=`kcmshell5 -v | awk '{print $2}'`
echo " Distro: $distro" >> $SIGFILE echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
TL;DR: Missing DISPLAY variable in enviornment from cron.
What happens in detail: - a script from cron runs in a minimal environment. - only very few environment variables are defined at all.
The command "kf5-config" wants / partially needs a working "DISPLAY".
The command "kcmshell5" needs a set "DISPLAY" variable or it fails.
Try with a extra line before / after the SIGFILE=.... line: [code] export DISPLAY=:0.0 [/code]
Other variables may be needed also.
try to start your script from an other script via a line: #/usr/bin/bash exec -c <your-sig-script>
to simulate a empty environment. also see "help exec" for details on the "-c" option.
- Yamaban.
Many thanks for the explanation re unavailable environment variables, and lots of suggestions to try. -- Bob Williams System: Linux 4.4.22-1-default Distro: openSUSE 42.2 Beta (x86_64) Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma: 5.8.0
Hi, Am Montag, 17. Oktober 2016, 16:33:05 CEST schrieb Bob Williams: [...]
echo " Desktop: $kde, $Qt and Plasma: $plasma" >> $SIGFILE exit 0
When run from my user command line it works correctly, but when I run it from my crontab, it fails to get the plasma version. So the last line of output reads:
Desktop: KDE Frameworks: 5.26.0, Qt: 5.6.1 and Plasma:
The error message returned by the system is:
QXcbConnection: Could not connect to display
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
I would run a query on the rpm database: rpm --qf %{VERSION} -q plasma5-desktop Greetings from Bremen hartmut -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2016-10-17 17:33, Bob Williams wrote:
I have a bash script to generate the sig featured below.
...
I'm guessing that cron is not running in graphical environment, but not sure why that would make a difference.
Can anyone suggest a solution/workaround, please?
Run another script each time you login into the desktop that saves that information into a file, which then is sourced by your cron file. /If/ it needs to be a cron job, I do not see things that change with time or must be recent, like "now listening to...". There is a directory that I don't remember the name that gets its content executed when you login to KDE, I think. -- Cheers / Saludos, Carlos E. R. (from 13.1 x86_64 "Bottle" at Telcontar)
participants (10)
-
Andrei Borzenkov
-
Bob Williams
-
Carlos E. R.
-
David T-G
-
Hartmut Meyer
-
ianseeks
-
jdd
-
John Andersen
-
Roger Price
-
Yamaban