Unix help please - ...
 

MegaSack DRAW - This year's winner is user - rgwb
We will be in touch

[Closed] Unix help please - an easy one

22 Posts
9 Users
0 Reactions
197 Views
Posts: 91097
Free Member
Topic starter
 

I want to set up a script for activating external monitors (don't ask.. just.. don't...) and bind it to the external monitor hotkey on my laptop (or some other perhaps, whatever).

There's a script to enable and a script to disable, but there's only one key of course so it needs to toggle. So then I need to know if the monitor's enabled or disabled.

I thought the easiest way would be to echo something like 'ON' to a file when I enable and 'OFF' when I disable. I tried echo "ON" >> file but for some reason I get permission denied, even though I use sudo, and sudo vi file works fine, I have permission.

Also - simply echoing to that file will just append - how to replace the file contents?

Actually though - thinking about it - if the system crashes or reboots - the file will still contain 'ON' even when the monitor defaults to disabled.. so perhaps I can use ps and what.. awk or something.. to check the presence of the second X server...


 
Posted : 02/07/2014 10:33 am
Posts: 8177
Free Member
 

Just create a new file and test for it's existence with your enable/disable script. Just touch an empty file called "monitor" or something, and have your script rename it accordingly when toggled - monitor.on and monitor.off or something

>> to append, > to overwite I think BTW


 
Posted : 02/07/2014 10:38 am
Posts: 401
Free Member
 

I think $DISPLAY should hold the info you want.

Or xset q


 
Posted : 02/07/2014 10:50 am
Posts: 91097
Free Member
Topic starter
 

Thanks folks.

The external monitor config requires me to start a second X server. It shows up differently in the output of ps, so maybe I should just use.. grep?


 
Posted : 02/07/2014 10:54 am
Posts: 3461
Full Member
 

ps -ef | grep <X_SERVER_PROCESS_NAME> | grep -v grep

echo $? will return 0 if it was found, and 1 if it wasn't.


 
Posted : 02/07/2014 11:01 am
Posts: 91097
Free Member
Topic starter
 

So in a shell script.. if <backwards apostrophe> grep gubbins <backwards apostrophe> != '' then ..?

Or whatever the hell the syntax is..


 
Posted : 02/07/2014 11:02 am
Posts: 3461
Full Member
 

[code]if [ <backwards apostrophe>ps -ef | grep <XSERVERNAME> | grep -v grep<backwards apostrophe> ]
then
echo "it's running!"
else
echo "it's not running!"
fi[/code]


 
Posted : 02/07/2014 11:05 am
Posts: 91097
Free Member
Topic starter
 

echo "cheers" >> verses


 
Posted : 02/07/2014 11:14 am
Posts: 3461
Full Member
 

Obviously not relevant now, but;

I tried echo "ON" >> file ...
Also - simply echoing to that file will just append - how to replace the file contents?

>> appends
> overwrites


 
Posted : 02/07/2014 11:23 am
Posts: 7090
Full Member
 

Can you use xrandr? It outputs information about the attached displays.

$ xrandr
Screen 0: minimum 320 x 200, current 1680 x 1050, maximum 8192 x 8192
DVI-I-1 connected 1680x1050+0+0 (normal left inverted right x axis y axis) 474mm x 296mm

etc.

EDIT: no, you can't as it won't tell the on/off state of the monitor, just the display settings.


 
Posted : 02/07/2014 11:27 am
Posts: 6898
Full Member
 

Nostalgic stuff.

SCO Openserver FTW!


 
Posted : 02/07/2014 11:30 am
Posts: 91097
Free Member
Topic starter
 

No, the ps grep command definitely works. I'm just farting about with basic commands.

Verses' thing is right but I seem to be having syntactical problems...


 
Posted : 02/07/2014 11:41 am
Posts: 91097
Free Member
Topic starter
 

#!/bin/bash
if [ ps -ef | grep "Xorg :8" | grep -v grep ]
then echo "Not running"
else
echo "Running"
fi

./test.sh: line 2: [: too many arguments
Running

EDIT.. ooh.. I think I see what's going on. It's evaluating the ps command and substituting it into the script.. that's not what I want


 
Posted : 02/07/2014 11:42 am
Posts: 8
Free Member
 

SCO openserver is utterly completely pants.

With badly-fitted knickers on.


 
Posted : 02/07/2014 11:43 am
Posts: 6208
Full Member
 

need some backticks i think
edit: oh they were probably there but made the forum s/w freak out


 
Posted : 02/07/2014 11:47 am
Posts: 1460
Full Member
 

I think you can do something like

result=$( ps -ef | grep "Xorg :8" | grep -v grep )
if [ $result ]
then echo "running";
else
echo "not running";
fi

Also, can't you set a global environmental variable instead of grep hackery?

E.g.

if [ $MONITOR_ON = true ]
export MONITOR_ON=false;
else
export MONITOR_ON=true;
fi

I don't know whether that works though I'm a bit rusty (EDIT: I just tried it actually and it only works in the terminal you run it in. As you were...)


 
Posted : 02/07/2014 11:48 am
Posts: 91097
Free Member
Topic starter
 

Right, it needed both single backwards and double quotes around the ps bit.

However.. whilst it's running my script properly, the program I actually need to run crashes when run via this hotkey, but not when run via a shell.


 
Posted : 02/07/2014 11:51 am
Posts: 91097
Free Member
Topic starter
 

Dear nVidia.. please develop proper Optimus support for Linux. Thanks, annoyed of Cardiff.


 
Posted : 02/07/2014 11:53 am
Posts: 91097
Free Member
Topic starter
 

Pff.. can't get it to work via hotkey.. too hard, not enough benefit.


 
Posted : 02/07/2014 11:57 am
Posts: 1460
Full Member
 

That's the spirit!


 
Posted : 02/07/2014 11:58 am
Posts: 8177
Free Member
 

Something like Autokey might work? Define 2 different keys for toggling the monitor on/off?

https://code.google.com/p/autokey/


 
Posted : 02/07/2014 11:59 am
Posts: 6208
Full Member
 

Guessing the script is being run sudo?
Hotkey binding in the Desktop Environment probably can't allow that (how do you put passwd in?)
Probably need to change owner or group to root or whatever X is arunning as, and set the setuid/setgid bits on the file, to make it run as a user with sufficient privilege?
could be talking BS though


 
Posted : 02/07/2014 12:07 pm
Posts: 7090
Full Member
 

You don't need to use setuid anymore, that's soooo 1990s.

sudo FTW!

Add a line like this to /etc/sudoers.conf

youruserid ALL=(ALL) NOPASSWD: /usr/bin/yourcommand


 
Posted : 02/07/2014 8:22 pm