when linux system calls scripts some commands don't work ( cron / if-up.d ) - linux

Hi I'm trying to run a script that calls xclip in order to have a string ready to paste when i connect to the internet.
I have a script /etc/network/if-up.d/script that does execute when connecting (i make him post a date in a file succesfuly ) but the xclip instruction seems not to work, there's nothing to paste. If i call this script manually by typing /etc/network/if-up.d/script in a console it works perfectly.
If i try to launch a zenity message it also don't appeare when connecting. Again if i do it by hand it appeares.
Then I have a expect script that calls matlab (console mode), if I execute it manually it works but if i call it from cron it freezees when calling the script.
It's driving me crasy since it seems that only certain commands in a script can be executed when the system calls them automaticaly.
I'v tryed to call the instructions with nohup instruction & but still misses

This is working as designed, you search around and will see compliated ways to resolve this issue, or you can use xmessage as I describe here: Using Zenity in a root incron job to display message to currently logged in user
Easy option 1: xmessage (in the script)
MSSG="/tmp/mssg-file-${RANDOM}"
echo -e " MESSAGE \n ==========\n Done with task, YEY. " > ${MSSG}
xmessage -center -file ${MSSG} -display :0.0
[[ -s ${MSSG} ]] && rm -f ${MSSG}
Easy option 2: set the DISPLAY (then should work)
export DISPLAY=:0 && /usr/bin/somedirectory/somecommand

question is answered here for cron :
http://ubuntuforums.org/archive/index.php/t-105250.html
and here for if-up network :
Bash script not working properly when run automatically

Related

Bash script with scrot area not working

I created a bash script to use for interactive screen capture and another one for window capture. I'm linking to these with keyboard shortcuts in Linux. The window capture script works without problems:
#!/bin/sh
scrot -u 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
But the script for for area capture (user selects area with mouse drag) does not work, even though the command works in terminal:
#!/bin/sh
scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
What am I doing wrong? Or maybe a better question is what is preventing the script from letting me select an area of the screen?
I manged to get it working by adding a delay to give the giblib resource time (2/10 of a second) to load:
#!/bin/sh
sleep 0.2 ; scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
How I found the solution:
I couldn't figure out how to get errors to output to a file because running my script from terminal didn't produce any errors. Double clicking the script ran properly and script > file 2>&1 in terminal didn't give me any errors because it ran properly from terminal. I only had errors when I tried to use the keyboard shortcuts (keybindings) attached to the second command from my original post. To see the error that finally lead to the above solution, I downloaded:
`apt-get install xbindkeys` && `apt-get install gconf-editor`
I ran gconf-editor and used the Run Action to executed the script the same manner it would be executed if I was using the keybindings...but attached to a terminal output. That gave me the error output I needed to see:
giblib error: couldn't grab pointer:Resource temporarily unavailable
Which lead me to this post:
https://bbs.archlinux.org/viewtopic.php?id=86507 for the tip.
For whomever the jtlindsey's answer did not work for solving the problem :
giblib error: couldn't grab pointer:Resource temporarily unavailable
Another solution could be this : just before calling scrot, run the command :
xdotool key XF86Ungrab
This should release the pointer, and the scrot command should work after it.
Note : the source claims that before executing previous xdotool command, it would possibly be required to execute :
setxkbmap -option grab:break_actions

Running a bash script which automatically opens a terminal on bootup [duplicate]

This question already has answers here:
UBUNTU: XOpenDisplay(NULL) fails when program run in boot sequence via rc.local
(2 answers)
Closed 7 years ago.
I have to run a bash script on bootup which opens couple of terminals and runs some command in each terminal.
test.sh (My bash file)
#!/bin/bash
sleep 10
gnome-terminal --tab-with-profile="Default" -e 'bash -c '\''export TAB=1; mkdir /home/naman/Desktop/test_folder'\'
I have created a testjob.conf inside /etc/init/ :
testjob.conf (Upstart config file)
description "A test job file for experimenting with Upstart"
author "Naman Kumar"
start on runlevel [2345]
exec echo Test Job ran at `date` >> /var/log/testjob.log
exec bash /home/naman/Desktop/test.sh
Now, the problem is testjob.conf is not able to run the test.sh file on bootup (or it runs it but does not create a folder test_folder). If I remove the last line from testjob.conf : exec bash /home/naman/Desktop/test.sh, everything works and when I do cat /var/log/testjob.log, I get the correct output but if the last line is there, cat /var/log/testjob.log does not give the latest output.
I have also tried updating /etc/rc.local with : bash /home/naman/Desktop/test.sh but that also does not seem to be running the test.sh script on bootup
I am not sure whether they run the scripts but are not able to create the folder or they are not even able to run the script on bootup.
Note: I can not use System -> Preferences -> Startup Applications because I don't have any monitor, so the desktop application does not run. (I am running it on a Single Board Computer with no monitor).
Does anyone know what is the issue here and why is the test.sh script not running properly on bootup?
Thanks in advance.
Naman
Cong ma is pretty close on this. Sysvinit is for system level daemons. It has no idea for users. That means it doesnt have a way to interact with your window system (or gnome).
Further: without a monitor, what would you expect gnome-terminal to do? gnome-terminal would open up a terminal on the monitor: which you can't see.
What you should look at is taking your commands (date, etc) and putting them in /etc/rc.local and not trying to 'olay them into a different terminal or anything. Just literally run the commands there.

Unable to run X11 (graphical) programs from atd

I am trying to schedule the execution of a shell-script with the Linux tool "at".
The shell script (video.sh) looks like this:
#!/bin/sh
/usr/bin/vlc /home/x/video.mkv
The "at" command:
at -f /home/x/video.sh -t 201411052225
When the time arrives, nothing happens.
I can execute the shell-script just fine via console or by rightclicking - Execute. VLC starts like it is supposed to. If I change the script to e.g. something simple like
#!/bin/sh
touch something.txt
it works just fine.
Any ideas, why "at" will not properly execute a script that starts a graphical program? How can I make it work?
You're trying to run an X command (a graphical program) at a scheduled time. This will be extremely difficult, and quite fragile, because the script won't have access to the X server.
At the very least, you will need to set DISPLAY to the right value, but even then, I suspect you will have issues with authorisation to use the X screen.
Try setting it to :0.0 and see if that works. But if you're logged out, or the screensaver's on, or any number of other things...
(Also, redirect vlc's stdout and stderr to a file so that you can see what went wrong.)
Your best bet might be to try something like xuserrun.
I suspect that atd is not running. You have to start the atd daemon before (and to set DISPLAY variable like chiastic-security said) ;)
You can test if atd is running with
pidof atd &>/dev/null && echo 'ATD started' || echo >&2 'ATD not started
Your vlc command should be :
DISPLAY=:0 /usr/bin/vlc /home/x/video.mkv
(Default display)

Command line script run in background goes in stopped state

I have a short php utility script, I run it from cli simply with:
php myscript.php
The script is always running, periodically performing some tasks (not relevant for the question). It doesn't need any input from the user.
After running it, I usually press CTRL+z and then run bg to put the process in background, and everything is fine.
If I run it as:
php myscript.php &
the script is put on background on start, but it is also put in a stopped state. Example:
[1] 11513
[1]+ Stopped php myscript.php
even running bg at this point doesn't help, I have to run fg, then CTRL+z and bg again to make it work.
This is the php script:
<?
while(true){
echo 'hi '.time()."\n";
sleep(30);
}
?>
My problem is that I cannot run it directly in background, because the system stops it, and I don't understand why. How can I fix this?
update:
I made a bash version of the same script, and it can be run and put in background (running and not stopped) just by launching it with the & in the end (script.sh &)
script.sh:
#!/bin/bash
while true; do
echo `date`
sleep 30
done
Why the php script is being stopped after launching it in background, and the bash script doesn't?
What could cause this different behaviour?
I found what is causing the issue. In PHP, if the readline module is enabled, any command line script will expect an input, even if the script is written to NOT wait for user input.
To check if you have readline support enabled, just run:
php --info |grep "Readline Support"
and check the output. If you get Readline Support => enabled then you have readline enabled and you may experience the problem described in the original question.
The proper way to use the cli when is then to explicitly specify that php is not using the terminal for input:
php myscript.php < /dev/null &
Further info: http://php.net/manual/en/book.readline.php
Alternatives:
./test.php >/dev/null &
or (more creative):
nohup php test.php > /dev/null 2>&1 &
(p.s.: I still believe this question belongs to ServerFault, btw.. problem solved!)
Usually, the process what do you send to background with the & and the script waiting for an input from the terminal, going into the stopped state.
E.g. have a bash script valecho:
#!/bin/sh
read val
echo $val
runnig it as:
./valecho &
the script will stop.
When run it as
echo hello | ./valecho &
will correctly run and finish.
So, check your php - probably wants some input from the stdin
Edit - based on comment:
i'm not an php developer - but just tried the next script (p.php)
<?php
while(true){
   echo 'hi '.time()."\n";
   sleep(3);
}
?>
with the command:
php -f p.php &
and running nicely... so... sry for confusion...
From http://php.net/manual/en/book.readline.php :
When readline is enabled, php switches the terminal mode to accept line-buffered input. This means that the proper way to use the cli when you pipe to an interactive command is to explicitly specify that php is not using the terminal for input:
php myscript.php < /dev/null &
source

iLO3: Multiple SSH commands

is there a way to run multiple commands in HPs integrated Lights-Out 3 system via SSH? I can login to iLO and run a command line by line, but I need to create a small shell-script, to connect to iLO and to run some commands one by one.
This is the line I use, to get information about the iLO-version:
/usr/bin/ssh -i dsa_key administrator#<iLO-IP> "version"
Now, how can I do something like this?
/usr/bin/ssh -i dsa_key administrator#<iLO-IP> "version" "show /map1 license" "start /system1"
This doesn't work, because iLO thinks it's all one command. But I need something to login into iLO, run these commands and then exit from iLO. It takes too much time to run them one after the other because every login into iLO-SSH takes ~5-6 seconds (5 commands = 5*5 seconds...).
I've also tried to seperate the commands directly in iLO after manual login but there is no way to use multiple commands in one line. Seems like one command is finished by pressing return.
iLO-SSH Version is: SM-CLP Version 1.0
The following solutions did NOT work:
/usr/bin/ssh -i dsa_key administrator#<iLO-IP> "version; show /map1 license; start /system1"
/usr/bin/ssh -i dsa_key administrator#<iLO-IP> "version && show /map1 license && start /system1"
This Python module is for HP iLO Management. check it out
http://pypi.python.org/pypi/python-hpilo/
Try putting your commands in a file (named theFile in this example):
version
show /map1 license
start /system1
Then:
ssh -i dsa_key administrator#iLO-IP < theFile
Semicolons and such won't work because you're using the iLO shell on the other side, not a normal *nix shell. So above I redirect the file, with newlines intact, as if you were typing all that into the session by hand. I hope it works.
You are trying to treat iLO like it's a normal shell, but its really HP's dopy interface.
That being said, the easiest way is to put all the commands in a file and then pipe it to ssh (sending all of the newline characters):
echo -e "version\nshow /map1 license\nstart /system1" | /usr/bin/ssh -i dsa_key administrator#<iLO-IP>
That's a messy workaround, but would you might fancy using expect? Your script in expect would look something like that:
# Make an ssh connection
spawn ssh -i dsa_key administrator#<iLO-IP>
# Wait for command prompt to appear
expect "$"
# Send your first command
send "version\r"
# Wait for command prompt to appear
expect "$"
# Send your second command
send "show /map1 license\r"
# Etc...
On the bright side, it's guaranteed to work. On the darker side, it's a pretty clumsy workaround, very prone to breaking if something goes not the way it should (for example, command prompt character would appear in version output, or something like that).
I'm on the same case and wish to avoid to run a lot of plink commands. So I've seen you can add a file with the -m option but apparently it executes just one command at time :-(
plink -ssh Administrator#AddressIP -pw password -m test.txt
What's the purpose of the file ? Is there a special format for this file ?
My current text file looks like below:
set /map1/oemhp_dircfg1 oemhp_usercntxt1=CN=TEST
set /map1/oemhp_dircfg1 oemhp_usercntxt2=CN=TEST2
...
Is there a solution to execute these two commands ?
I had similar issues and ended up using the "RIBCL over HTTPS" interface to the iLO. This has advantages in that it is much more responsive than logging in/out over ssh.
Using curl or another command-line HTTP client try:
USERNAME=<YOUR_ILO_USERNAME>
PASSWORD=<YOUR_ILO_PASSWORD>
ILO_URL=https://<YOUR_ILO_IP>/ribcl
curl -k -X POST -d "<RIBCL VERSION=\"2.0\">
<LOGIN USER_LOGIN=\"${USERNAME}\" PASSWORD=\"${PASSWORD}\">
<RIB_INFO MODE="READ">
<GET_FW_VERSION/>
<GET_ALL_LICENSES/>
</RIB_INFO>
<SERVER_INFO MODE=\"write\">
<SET_HOST_POWER HOST_POWER=\"Yes\">
</SERVER_INFO>
</LOGIN>
</RIBCL>" ${ILO_URL}
The formatting isn't exactly the same, but if you have the ability to access the iLO via HTTPS instead of only ssh, this may give you some flexibility.
More details on the various RIBCL commands and options may be found at HP iLO 3 Scripting Guide (PDF).

Resources