From Mageia wiki
Jump to: navigation, search
Currently, live version of this page is: Configuring_autostart_with_MATE_in_Mageia

Introduction

This page describes how to configure autostart with the MATE desktop environment in Mageia Linux. "autostart" enables starting programs automatically at login.

For example: starting a terminal, a monitoring program, a web browser


Benefits

Using autostart can save time especially if you tend to manually start the same few programs after logging in. It is easy to script what you need started and much faster than doing so manually. It is also possible to place and size program windows in required workspaces (for multi-space DEs).


Preparation

The autostart configuration consists of two parts:

  1. Configuration file in ${HOME}/.config/autostart/
  2. A script to position, size, and launch the programs you wish to start on login

This page gives examples of both.

The example autostart script shown here uses wmctrl to switch between workspaces. This is not installed by default in Mageia 7.

Install:

urpmi wmctrl


Configuration in ${HOME}/.config/autostart/

Note that files in this directory to be used for autostart need to be named with a suffix of .desktop.

Example:

[mpb@localhost ~]$ nl -ba /home/mpb/.config/autostart/MATE_autostart.desktop 
     1	[Desktop Entry]
     2	Name=MATE_autostart
     3	Comment=MATE_autostart: invokes script to start programs at login to MATE
     4	Exec=/home/mpb/bin/MATE_initrc
     5	Icon=mate-desktop
     6	Terminal=false
     7	Type=Application
     8	StartupNotify=true
     9	X-GNOME-Autostart-enabled=true

NB The Exec on line 4 (above) refers to the script that is used to launch programs on login. In this case, it is a script in /home/mpb/bin/MATE_initrc. This will need to be modified for your own choice.


Autostart script

Since the script is run invisibly to the user, it is useful to log script activity. This can help with debugging or adapting to specific environments and requirements.

The example script MATE_initrc (see Appendix-1) logs to ${HOME}/logs/MATE_initrc_log.

MATE_initrc example in Appendix-1 does the following with a brief pause on each different workspace as programs are started:

  1. open multi-tabbed mate-terminal and gkrellm (monitor display) in workspace 1
  2. Launch google-chrome-stable in workspace 3
  3. Launch Firefox in workspace 4
  4. Returns to workspace 1


Modify MATE_initrc for your needs

Review your local copy of MATE_initrc (see Appendix-1).

Since this is for your own login configuration it is appropriate to keep this in ${HOME}/bin/MATE_initrc.

Things to consider changing are marked with a #LOCAL tag:


The setting of ${default_notify}

Find the following line:

default_notify=Your.email@wherever.org    # set to email to be notified of errors #LOCAL

Should be modified to be the email address you would like any failure log report from MATE_initrc to be sent to.


Section which autostarts programs

The section which sets workspaces, geometries and sizes of programs to be auto started. You may wish to choose different programs or workstation placement, geometries, sizes.

NB when running this on a slow or older computer you probably need to increase the pause time before switching to aother workspaces.

Example:

# Now start the programs we wish to have autostarted on login ---------------------------
#
#LOCAL modify this section as needed to autostart which programs are useful to you

pause=3     # time to pause between switching workspaces for startups  #LOCAL

doit "wmctrl -s 0  # switch to workspace 1"

# notify user that MATE_autostart has commenced
doit "zenity --notification --title MATE_autostart --text='MATE_autostart commenced'"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'"

# start multi-tabbed mate terminal
doit "/usr/bin/mate-terminal --geometry=191x84+146+30 --tab --tab --tab --tab --tab --tab --tab &"


# start up gkrellm
doit "/usr/bin/gkrellm --geometry +1800+30 &"
doit "sleep ${pause}      # pause for mate-terminal and gkrellm to start up"


doit "wmctrl -s 2  # switch to workspace 3"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 3'"

doit "/usr/bin/google-chrome-stable %U &"
doit "sleep ${pause}      # pause for google-chrome-stable to start up"
doit "wmctrl -i -r $(wmctrl -lp | grep "Google Chrome" | awk '{print $1}') -e 0,150,30,1756,980 # position and resize Google Chrome"
doit "sleep ${pause}      # pause to let user see reposition and resize of Firefox"


doit "wmctrl -s 3  # switch to workspace 4"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 4'"

doit "~/Downloads/firefox/firefox&"
doit "sleep ${pause}      # pause to let Firefox start up"
doit "wmctrl -i -r $(wmctrl -lp | grep "Mozilla Firefox"| awk '{print $1}') -e 0,150,30,1756,1024    # position and resize Mozilla Firefox"
doit "sleep ${pause}      # pause to let user see reposition and resize of Firefox"


doit "wmctrl -s 0  # switch to workspace 1 - return to workspace with mate-teminal and gkrellm"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'"

doit "sleep ${pause}"     # optional: leaves "completed notification on screen a little longer

# notify user that MATE_autostart has completed
doit "zenity --notification --title MATE_autostart --text='MATE_autostart completed'"

# tidying up and ending ------------------------------------------------------------------

Appendix-1: Autostart script example MATE_initrc

#!/bin/sh

# Name          MATE_initrc
# Purpose       Script to be run automatically on login to start up useful programs in MATE desktop
# Usage         Normally invoked at login to MATE desktop via: ${HOME}/.config/autostart/MATE_autostart.desktop
#
# Author        paul[dot]blackburn[at]gmail[dot]com
# Written       2020_03_03
# Updated       Thu 12 Mar 18:51:42 GMT 2020 
# 2020_03_12     redo position/resize for Chrome and Firefox
# 2020_03_09	bugfix the "cat << \#\#EEOOFF" line - evaluation of ${HOME} via sed
# 2020_03_09	Now reposition and resize Firefox
# 2020_03_08    Added zenity notification on each switch of workspace
# 2020_03_08	Now renaming previous log to <date>_log
# 2020_03_07    Added zenity notifications at start and end of program start section
#
# Example of ${HOME}/.config/autostart/MATE_autostart.desktop follows

# cat << \#\#EEOOFF | tail -9 | sed -e "s/^................//" -e "s,\${HOME},${HOME}," > ${HOME}/.config/autostart/MATE_autostart.desktop
## [user@localhost ~]$ nl .config/autostart/MATE_autostart.desktop | sed -e "s/^./## &/"
##      1       [Desktop Entry]
##      2       Name=MATE_autostart
##      3       Comment=MATE_autostart.desktop: invokes script to start programs at login to MATE
##      4       Exec=${HOME}/bin/MATE_initrc
##      5       Icon=mate-desktop
##      6       Terminal=false
##      7       Type=Application
##      8       StartupNotify=false
##      9       X-GNOME-Autostart-enabled=true
##EEOOFF

#  To create file MATE_autostart.desktop from the above:
#  copy from the "cat" (not including the leading "#") to "##EEOOFF"
#  and paste to your shell to create MATE_autostart.desktop.
#  Then verify line 4 "Exec" is set to your correct ${HOME}.

#LOCAL		localizations: search for "#LOCAL" (below) and modify for your own needs:
#		  default_notify=Your.email@wherever.org    # set to email to be notified of errors
#		Note: on older, slower systems may need to increase the pause time "pause=2"

# Disable       To disable autostart: delete (or move) ~/.config/autostart/MATE_autostart.desktop
#               example: mv ~/.config/autostart/MATE_autostart.desktop ~/
#
# Re-enable     To re-enable, just move the MATE_autostart.desktop file back to ~/.config/autostart/
#               example: mv ~/MATE_autostart.desktop ~/.config/autostart/
#
# References:
#		https://wiki.mageia.org/en/Configuring_autostart_with_MATE_in_Mageia
#               https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s07.html
#               https://askubuntu.com/questions/598195/how-to-add-a-script-to-startup-applications-from-the-command-line


# functions -------------------------------------------------------------

usage() {

        cat <<eeooff
${cmd} runs commands needed on login and starting MATE desktop

${cmd} is normally run at login via ${HOME}/.config/autostart/MATE_autostart.desktop
Please report bugs/suggestions/comments to: ${default_notify}
eeooff

}

fatal() {
        echo "${cmd} fatal: ${1}" >&2
        exit 1
}

error() {
        echo "${cmd} error: ${1}" >&2
}

warning() {
        echo "${cmd} warning: ${1}" >&2
}

tstamp() {
        echo $(date +%Y_%m_%d_at_%H:%M:%S) ${cmd}: ${1}
}

doit() {
        tstamp "${1}"
        eval ${1}

        retcode=$?
        if [ ${retcode} != 0 ]; then
                error "\$?=${retcode}"
        fi
}

elapsed_time () {
        start=${1}
        end=${2}

#       echo debug: elapsed_time ${start} ${end}
        unset x
        unset y

        if [ ${start} = ${end} ]; then
                echo -n "zero seconds"
        fi

        let elapsed_seconds=${end}-${start}
        let days=${elapsed_seconds}/86400
        let days_sec=${days}*86400

        let x=${elapsed_seconds}-${days_sec}
        let hours=${x}/3600
        let hours_sec=${hours}*3600
       
        let x=${days_sec}+${hours_sec}
        let y=${elapsed_seconds}-${x}
        let mins=${y}/60
        let mins_sec=${mins}*60

        let x=${days_sec}+${hours_sec}+${mins_sec}
        let secs=${elapsed_seconds}-${x}

        if [ ${days} != 0 ]; then
                if [ ${days} = 1 ]; then
                        echo -n "1 day "
                else
                        echo -n "${days} days "
                fi
        fi

        if [ ${hours} != 0 ]; then
                if [ ${hours} = 1 ]; then
                        echo -n "1 hour "
                else
                        echo -n "${hours} hours "
                fi
        fi


        if [ ${mins} != 0 ]; then
                if [ ${mins} = 1 ]; then
                        echo -n "1 minute "
                else
                        echo -n "${mins} minutes "
                fi
        fi

        if [ ${secs} != 0 ]; then
                if [ ${secs} = 1 ]; then
                        echo -n "1 second "
                else
                        echo -n "${secs} seconds"
                fi
        fi

        echo
}

trap_cleanup () {                # tidy up on receipt of signal (ctrl-C, etc)
        tstamp "trap_cleanup commenced"

        doit "rm ${lockfile}"

        epoch_now=$(date +%s)
        tstamp "total duration $(elapsed_time ${start_epoch} ${epoch_now})"

        tstamp "trap_cleanup completed"
        exit 1
}

# end functions section

# set initial values ----------------------------------------------------------------------

version=8
cmd=$(basename $0)
cmdline="${cmd} $*"
logdir=~/logs
log=${logdir}/${cmd}_log

lockfile=/tmp/${cmd}_lockfile
tempfile=/tmp/${cmd}_tempfile_$$

# Configure the following for your needs: #LOCAL

default_notify=Your.email@wherever.org    # set to email to be notified of errors #LOCAL


# main starts here ------------------------------------------------------------------------

start_epoch=$(date +%s)

# set interrupt signal trap routine

for signal in 1 2 3 15; do
trap "echo \"interrupted by signal ${signal}\"; trap_cleanup; exit 1" ${signal}
done

case "${1}" in
        -? | -help | -usage | --?| --help | --usage )
                usage
                exit
                ;;
esac

mkdir -p ${logdir} 2>/dev/null          # make sure we have a $logdir

#echo "stdout and stderr now being written to ${log}+"
exec 4>&2
exec 3>&1
exec 1>${log}+
exec 2>&1

tstamp "commenced $(dirname ${0})/${cmd} version ${version} on $(date '+%a %d %h %y')"


# sanity checking starts here

tstamp "Check we are running on Linux"
os=$(uname)

if [ "${os}" != "Linux" ]; then
        fatal "This is ${os} but we want Linux! Bye-bye!"
else
        tstamp "Good news: this is Linux and we are happy penguins! :-)"
fi

tstamp "Check for machine type: only supported on x86"
machine=$(uname -m)
case ${machine} in
        i386 | i486 | i586 | i686 | x86_64 )
                tstamp "Good! Supported machine type: ${machine}"
                ;;
        * )
                warning "Hmm? Unknown machine type: ${machine}"
                ;;
esac
#tstamp "Test for Linux distribution type"

distro="unknown"
file=/etc/redhat-release
if [ -s ${file} ]; then
        distro="RedHat"
fi

file=/etc/SuSE-release
if [ -s ${file} ]; then
        distro="SuSE"
fi

file=/etc/mandrake-release
if [ -s ${file} ]; then
        distro="Mandrake"
fi

file=/etc/mandriva-release
if [ -s ${file} ]; then
        distro="Mandriva"
fi

file=/etc/mageia-release
if [ -s ${file} ]; then
        distro="Mageia"
fi

file=/etc/debian_version
if [ -s ${file} ]; then
        distro="Debian"
fi

file=/etc/slackware-version
if [ -s ${file} ]; then
        distro="Slackware"
fi

file=/etc/turbolinux-version
if [ -s ${file} ]; then
        distro="TurboLinux"
fi


#tstamp "Test for Linux distribution version"
case ${distro} in
        RedHat )        distro_version=$(cat /etc/redhat-release)
                        ;;
        SuSE )          distro_version=$(cat /etc/SuSE-release)
                        ;;
        Mandrake )      distro_version=$(cat /etc/mandrake-release)
                        ;;
        Mandriva )      distro_version=$(cat /etc/mandriva-release)
                        ;;
        Mageia )        distro_version=$(cat /etc/mageia-release)
                        ;;
        Debian )        distro_version=$(cat /etc/debian_version)
                        ;;
        Slackware )     distro_version=$(cat /etc/slackware-version)
                        ;;
        TurboLinux )    distro_version=$(cat /etc/turbolinux-version)
                        ;;
        * )             distro_version="unknown"
                        ;;
esac

tstamp "Distribution is: ${distro_version}"


# crack command line arguments
while [ ! -z "${1}" ]; do
        case ${1} in
                -notify )
                        shift
                        if [ -z "${1}" ]; then
                                fatal "missing notify email address"
                        else
                                notify=${1}
                        fi
                        ;;
                -notify*)
                        notify=$(echo ${1} | cut -c8-)
                        ;;
                -help | -? | -usage )
                        usage
                        exit
                        ;;
                *)
                        warning "unknown command line argument: ${1}"
                        usage
                        exit 1
                        ;;
        esac
        shift
done

if [ -z "${notify}" ]; then
        notify=${default_notify}
fi

# If we reached here then it is likely we can complete a normal run
# but we need to switch the logfile.
# Start by renaming an existing previous log by adding a date prefix

if [ -s ${log} ]; then
	old_log=$(dirname ${log})/$(date +%Y_%m_%d_at_%H:%M:%S)_$(basename ${log})
	doit "mv -f ${log} ${old_log}    # rename previous log to <date>_log "
fi
doit "mv ${log}+ ${log}    # log cycling"

# end of (most) sanity checking ---------------------------------------


failed="false"

#LOCAL - If you change the programs started in the autostart section then you need to also update "command_list=" below
tstamp "Checking if we have the commands we need"
command_list="mate-terminal gkrellm /usr/bin/google-chrome-stable ${HOME}/Downloads/firefox/firefox wmctrl zenity"

for command in ${command_list}; do
#       tstamp "checking we have ${command} command"
        which ${command} 2>&1 > /dev/null
        if [ $? = 0 ]; then
                tstamp "Good, we have ${command}: $(${command} --version)"
        else
                warning "missing command: ${command}"
                warning "Please install ${command} and try again"
                failed="true"
        fi
done

# At this point, "failed" may have been set to true but we will just do what we can (not exit here)

if [ ${failed} = "true" ]; then
	tstamp "Fail detected but attempting to continue"
fi

# Now start the programs we wish to have autostarted on login ---------------------------
#
#LOCAL modify this section as needed to autostart which programs are useful to you

pause=3     # time to pause between switching workspaces for startups  #LOCAL

doit "wmctrl -s 0  # switch to workspace 1"

# notify user that MATE_autostart has commenced
doit "zenity --notification --title MATE_autostart --text='MATE_autostart commenced'"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'"

# start multi-tabbed mate terminal
doit "/usr/bin/mate-terminal --geometry=191x84+146+30 --tab --tab --tab --tab --tab --tab --tab &"


# start up gkrellm
doit "/usr/bin/gkrellm --geometry +1800+30 &"
doit "sleep ${pause}      # pause for mate-terminal and gkrellm to start up"


doit "wmctrl -s 2  # switch to workspace 3"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 3'"

doit "/usr/bin/google-chrome-stable %U &"
doit "sleep ${pause}      # pause for google-chrome-stable to start up"
doit "wmctrl -i -r $(wmctrl -lp | grep "Google Chrome" | awk '{print $1}') -e 0,150,30,1756,980 # position and resize Google Chrome"
doit "sleep ${pause}      # pause to let user see reposition and resize of Firefox"


doit "wmctrl -s 3  # switch to workspace 4"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 4'"

doit "~/Downloads/firefox/firefox&"
doit "sleep ${pause}      # pause to let Firefox start up"
doit "wmctrl -i -r $(wmctrl -lp | grep "Mozilla Firefox"| awk '{print $1}') -e 0,150,30,1756,1024    # position and resize Mozilla Firefox"
doit "sleep ${pause}      # pause to let user see reposition and resize of Firefox"


doit "wmctrl -s 0  # switch to workspace 1 - return to workspace with mate-teminal and gkrellm"
doit "zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'"

doit "sleep ${pause}"     # optional: leaves "completed notification on screen a little longer

# notify user that MATE_autostart has completed
doit "zenity --notification --title MATE_autostart --text='MATE_autostart completed'"

# tidying up and ending ------------------------------------------------------------------

end_epoch=$(date +%s)

tstamp "total duration $(elapsed_time ${start_epoch} ${end_epoch})"
tstamp "completed"

# Switch stdout and stderr back to report results ONLY if something failed

if [ "${failed}" = "true" ]; then
        exec 1>&3
        exec 2>&4
        < ${log} mail -s "${cmd} on $(hostname)" ${notify}
fi

Appendix-2: example logfile from MATE_initrc

Here is an example of the logfile written when the MATE_initrc script (example above) is run.

Note that if any of the prorgams that are autostarted this way output error messages they will be captured here.

This can be useful to review and research any bugs or issues revealed when programs are auto started. In the example below, error messages about FontService from Google Chrome are captured.

[mpb@localhost ~]$ nl -ba logs/MATE_initrc_log 
     1	2020_03_12_at_18:58:23 MATE_initrc: commenced /home/mpb/bin/MATE_initrc version 8 on Thu 12 Mar 20
     2	2020_03_12_at_18:58:23 MATE_initrc: Check we are running on Linux
     3	2020_03_12_at_18:58:23 MATE_initrc: Good news: this is Linux and we are happy penguins! :-)
     4	2020_03_12_at_18:58:23 MATE_initrc: Check for machine type: only supported on x86
     5	2020_03_12_at_18:58:23 MATE_initrc: Good! Supported machine type: x86_64
     6	2020_03_12_at_18:58:23 MATE_initrc: Distribution is: Mageia release 7 (Official) for x86_64
     7	2020_03_12_at_18:58:23 MATE_initrc: mv -f /home/mpb/logs/MATE_initrc_log /home/mpb/logs/2020_03_12_at_18:58:23_MATE_initrc_log # rename previous log to <date>_log
     8	2020_03_12_at_18:58:23 MATE_initrc: mv /home/mpb/logs/MATE_initrc_log+ /home/mpb/logs/MATE_initrc_log # log cycling
     9	2020_03_12_at_18:58:23 MATE_initrc: Checking if we have the commands we need
    10	2020_03_12_at_18:58:24 MATE_initrc: Good, we have mate-terminal: MATE Terminal 1.22.1
    11	2020_03_12_at_18:58:24 MATE_initrc: Good, we have gkrellm: gkrellm 2.3.10
    12	2020_03_12_at_18:58:24 MATE_initrc: Good, we have /usr/bin/google-chrome-stable: Google Chrome 80.0.3987.132
    13	2020_03_12_at_18:58:24 MATE_initrc: Good, we have /home/mpb/Downloads/firefox/firefox: Mozilla Firefox 74.0
    14	2020_03_12_at_18:58:24 MATE_initrc: Good, we have wmctrl: 1.07
    15	2020_03_12_at_18:58:24 MATE_initrc: Good, we have zenity: 3.32.0
    16	2020_03_12_at_18:58:24 MATE_initrc: wmctrl -s 0 # switch to workspace 1
    17	2020_03_12_at_18:58:24 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart commenced'
    18	2020_03_12_at_18:58:24 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'
    19	2020_03_12_at_18:58:24 MATE_initrc: /usr/bin/mate-terminal --geometry=191x84+146+30 --tab --tab --tab --tab --tab --tab --tab &
    20	2020_03_12_at_18:58:24 MATE_initrc: /usr/bin/gkrellm --geometry +1800+30 &
    21	2020_03_12_at_18:58:24 MATE_initrc: sleep 3 # pause for mate-terminal and gkrellm to start up
    22	2020_03_12_at_18:58:27 MATE_initrc: wmctrl -s 2 # switch to workspace 3
    23	2020_03_12_at_18:58:27 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart workspace 3'
    24	2020_03_12_at_18:58:28 MATE_initrc: /usr/bin/google-chrome-stable %U &
    25	2020_03_12_at_18:58:28 MATE_initrc: sleep 3 # pause for google-chrome-stable to start up
    26	[24725:24725:0312/185828.376964:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
    27	[24750:1:0312/185828.653805:ERROR:child_process_sandbox_support_impl_linux.cc(79)] FontService unique font name matching request did not receive a response.
    28	[24750:1:0312/185828.654192:ERROR:child_process_sandbox_support_impl_linux.cc(79)] FontService unique font name matching request did not receive a response.
    29	2020_03_12_at_18:58:31 MATE_initrc: wmctrl -i -r 0x00800001 -e 0,150,30,1756,980 # position and resize Google Chrome
    30	2020_03_12_at_18:58:31 MATE_initrc: sleep 3 # pause to let user see reposition and resize of Firefox
    31	[24799:1:0312/185832.963288:ERROR:child_process_sandbox_support_impl_linux.cc(79)] FontService unique font name matching request did not receive a response.
    32	[24799:1:0312/185832.963558:ERROR:child_process_sandbox_support_impl_linux.cc(79)] FontService unique font name matching request did not receive a response.
    33	2020_03_12_at_18:58:34 MATE_initrc: wmctrl -s 3 # switch to workspace 4
    34	2020_03_12_at_18:58:34 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart workspace 4'
    35	2020_03_12_at_18:58:34 MATE_initrc: ~/Downloads/firefox/firefox&
    36	2020_03_12_at_18:58:34 MATE_initrc: sleep 3 # pause to let Firefox start up
    37	2020_03_12_at_18:58:37 MATE_initrc: wmctrl -i -r 0x04400003 -e 0,150,30,1756,1024 # position and resize Mozilla Firefox
    38	2020_03_12_at_18:58:37 MATE_initrc: sleep 3 # pause to let user see reposition and resize of Firefox
    39	2020_03_12_at_18:58:40 MATE_initrc: wmctrl -s 0 # switch to workspace 1 - return to workspace with mate-teminal and gkrellm
    40	2020_03_12_at_18:58:40 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart workspace 1'
    41	2020_03_12_at_18:58:40 MATE_initrc: sleep 3
    42	2020_03_12_at_18:58:43 MATE_initrc: zenity --notification --title MATE_autostart --text='MATE_autostart completed'
    43	2020_03_12_at_18:58:43 MATE_initrc: total duration 20 seconds
    44	2020_03_12_at_18:58:43 MATE_initrc: completed