I use the command wmctrl -n4 manually to create 4 desktops each time my Raspberry Pi reboots. My OS version info are as follows:

$ cat /etc/issue
Raspbian GNU/Linux 9 \n \l

$ uname -a
Linux raspberrypi 4.9.47-v7+ #1033 SMP Mon Sep 4 21:48:16 BST 2017 armv7l GNU/Linux

I tried to put this command /usr/bin/wmctrl -n4 to /etc/rc.local, or ~pi/.bashrc, neither ever worked. So is there a way to avoid the manual work on each reboot? Thanks.

2 Answers 2


The problem is that wmctrl needs to talk to the X server. So you don't want it to run at startup (at least not when rc.local runs), you want it to run when the X server launches. In addition, you need it to know what DISPLAY it should be talking to.

Raspberry pi autostart of LXDE does not work has background information about starting programs when the X server does. It appears though that when the autostart script fires, the desktops still aren't ready for wmctrl to control. So the best thing is to write a small script that introduces a short delay. I used 10 seconds and it was fine. You could probably get away with less most of the time, but I wouldn't think you'd need much less.

Create the script and make it executable. I placed the script in the user's "bin" directory

mkdir ~/bin
printf '#!/bin/bash\nsleep 10\nwmctrl -n4\n' > ~/bin/wmscript
chmod a+x ~/bin/wmscript

Tell the X session to run the script. Add the path to the script with an @ symbol at the beginning of the line.

printf "@bin/wmscript" >> ~/.config/lxsession/LXDE-pi/autostart

Now the next time you start an X session (logout or reboot), it should set your desktops a few seconds after login.


From the link below:

  1. Put a script containing the command in your /etc directory. Create a script such as "startup.sh" using your favorite text editor. Save the file in your /etc/init.d/ directory. Change the permissions of the script (to make it executable) by typing "chmod +x /etc/init.d/mystartup.sh".

Here is the original link: http://smallbusiness.chron.com/run-command-startup-linux-27796.html

Wrap your command into a basic bash/shell script and then follow the instructions above. If you need further assistance, just comment and I can walk you through it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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