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.

retnikt
  • 192
  • Here are another two possible answers: https://askubuntu.com/a/890253/566421 and https://askubuntu.com/a/975552/566421 – pa4080 Apr 06 '20 at 20:16

2 Answers2

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
Gryu
  • 7,559
  • 9
  • 33
  • 52
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.

Fjor
  • 300