Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 2 graphics cards on my laptop, one is IGP and another discrete, I've written a shell script to to turn off discrete graphics card. How I can convert it to systemd script, to runt at start-up?

share|improve this question
add comment

1 Answer

up vote 27 down vote accepted

There are mainly two approaches to do that:

  • If you have to run a script, you don't convert it but rather call the script through a systemd service.

Therefore you need two files: the script and the "service" file. Let's say your script is called vgaoff. Place your script in /usr/lib/systemd/scripts, make it executable. Then create a new service file in /usr/lib/systemd/system (a plain text file, let's call it vgaoff.service). So this is the location of your files:

  1. the script: /usr/lib/systemd/scripts/vgaoff
  2. the service: /usr/lib/systemd/system/vgaoff.service

Now you have to edit the service file. Its content depends on how your script works:

If vgaoff just powers off the gpu, e.g.:

exec blah-blah pwrOFF etc 

then the content of vgaoff.service should be:

[Unit]
Description=Power-off gpu

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff

[Install]
WantedBy=multi-user.target

If vgaoff is used to power off the GPU and also to power it back on, e.g.:

start() {
  exec blah-blah pwrOFF etc
}

stop() {
  exec blah-blah pwrON etc
}

case $1 in
  start|stop) "$1" ;;
esac

then the content of vgaoff.service should be:

[Unit]
Description=Power-off gpu

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff start
ExecStop=/usr/lib/systemd/scripts/vgaoff stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Once you have the files in place and configured you can enable the service:

systemctl enable vgaoff.service

It should then start automatically after rebooting the machine.

  • For the most trivial cases, you can do without the script and execute a certain command directly through the .service file:

To power off:

[Unit]
Description=Power-off gpu

[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch"

[Install]
WantedBy=multi-user.target

To power off & on:

[Unit]
Description=Power-off gpu

[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch"
ExecStop=/bin/sh -c "echo ON > /whatever/vga_pwr_gadget/switch"
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
share|improve this answer
    
Thanks for detailed answer. The script contains one command "echo OFF > /sys/kernel/debug/vgaswitcheroo/switch" –  Sharique Sep 11 '12 at 10:04
    
it fails with following message vgaoff.service - Power-off gpu Loaded: loaded (/usr/lib/systemd/system/vgaoff.service; enabled) Active: failed (Result: exit-code) since Tue, 11 Sep 2012 23:46:46 +0530; 30s ago Process: 5258 ExecStart=/usr/lib/systemd/scripts/vgaoff (code=exited, status=203/EXEC) CGroup: name=systemd:/system/vgaoff.service –  Sharique Sep 11 '12 at 18:17
1  
@Sharique - make sure your script is executable: chmod +x /usr/lib/systemd/scripts/vgaoff and the shebang line is not missing. –  don_crissti Sep 11 '12 at 19:16
    
oops,not again, I missed this small thing, it is working fine.. thanks again. –  Sharique Sep 12 '12 at 18:58
    
Thanks for your detailed and useful guide, but one command is IMHO dangerous: /bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch" overwrites /whatever/vga_pwr_gadget/switch with the text "OFF". shouldn't it be & instead of >, which would be: /bin/sh -c "echo OFF & /whatever/vga_pwr_gadget/switch"? Or without the echo part at all? (that's the way I use it successfully.) –  Bazon May 13 '13 at 19:01
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.