4

I already checked several tutorials, but I can't run my script when starting the operating system. I already put the file in /etc/init.d , /boot, /bin, /urs/bin . I think I already put it in every possible place. here's my code.

#!/bin/bash
echo "Starting command to put resolution on 1920x1080"
echo "Password_of_sudo" | sudo -S xrandr --newmode "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync && 
sudo xrandr --addmode Virtual1 1920x1080_60.00 && 
sudo xrandr --output Virtual1 --mode 1920x1080_60.00
echo "Finished.

Note: runs within a VM. And it works when starts manually, the problem is that i don't want to run manually i want it to start when the system loads.

11
  • Create a /etc/systemd/system/xrandr.service file (you could specify the service name you like) using:

    sudo -H gedit /etc/systemd/system/xrandr.service
    
  • Put the next inside of it:

    [Unit]
    After=multi-user.target
    
    [Service]
    User=your_user
    ExecStart=/home/your_user/scripts/xrandr.sh
    
    [Install]
    WantedBy=default.target
    
  • Place your script into /home/your_user/scripts folder or specify path to your script.

  • sudo chmod 664 /etc/systemd/system/xrandr.service
  • sudo systemctl enable xrandr.service
  • Check if it is working by sudo systemctl start xrandr.service

Also, you could modify your file the next way:

#!/bin/bash
echo "Starting command to put resolution on 1920x1080" | logger
export DISPLAY=:0 && xhost + local
echo "Password_of_sudo" | sudo -S xrandr --newmode "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync && 
sudo xrandr --addmode Virtual1 1920x1080_60.00 && 
sudo xrandr --output Virtual1 --mode 1920x1080_60.00
echo "Finished." | logger

But it is better do not use sudo or admin privileges if commands do not require it:

#!/bin/bash
echo "Starting command to put resolution on 1920x1080" | logger
export DISPLAY=:0 
xrandr --newmode "1920_new_name" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync && 
xrandr --addmode Virtual1 1920x1080_60.00 && 
xrandr --output Virtual1 --mode 1920x1080_60.00
echo "Finished." | logger

Check your display by echo $DISPLAY, then if you have not :0, then change it in your script.

To see what is wrong with your script, you could use the next command for monitoring in terminal:

$ journalct -f
| improve this answer | |
  • 5
    I would suggest not to do sudo with plain text password but to run the script as a user with sufficient privileges to begin with. – Carsten S Apr 7 at 11:49
  • thank you very much it worked right. I will remove this part of the sudo command – HigorLucas125 Apr 8 at 15:44
2

As this is a command meant to be run as root at system start, you could add it as a cron job launched by root (I used higor as your user, vm as your system name) with the following:

higor@vm$ sudo su -                       #this changes to root temporarily
[sudo] password for higor:                #enter your password
root@vm# cp /home/higor/script.sh /root   #copy the script to /root home
root@vm# chown root:root script.sh        #make the script property of root
root@vm# chmod u+x script.sh              #make the script executable
root@vm# VISUAL=nano crontab -e           #uses nano to edit crontab config of root

then add the following line:

@reboot /root/script.sh >/root/script.sh.log 2>&1

Save the file, and return to user higor with the command exit.

This line will be processed each system restart as root (you could learn more about cron configuration using the command man 5 crontab), and write any messages or errors to the file /root/script.sh.log. As this script already runs as root, doesn't need launching commands inside it with sudo; simply write the commands themselves.

| improve this answer | |
  • thank you very much, I will do this – HigorLucas125 Apr 8 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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