0

Context

Suppose one would like a single Bash installation script named install.sh to ensure Ubuntu 22.04 runs the command: picard (which launches a GUI), at boot (in the background, without showing the GUI).

Approach

I looked at: https://askubuntu.com/a/816/846880 which allows one to do this manually by typing:

crontab -e
[1] (to select the favourite editor)
@reboot /path/to/script

However, I would like the cronjob to be set up automatically instead of manually. So I looked in GitHub: and found this install script which contains:

#para crontab -e
#echo "@reboot sudo /home/pi/ili9342-driver/fbcp-ili9342" >> mycron; crontab mycron;rm mycron

Issue

However, that does not take into account whether the desired line already is in the crontab.

Question

Hence, I was wondering to know how to make a single Bash script automatically run the command picard at boot? (Ideally in the background without showing the GUI)

a.t.
  • 317
  • 1
    You can check if the desired line is already in the crontab by the command crontab -l | grep "some string in the desired line". Then use conditional logic to do the next step. – user68186 Jun 13 '22 at 22:08
  • 1
    See https://stackoverflow.com/questions/878600/how-to-create-a-cron-job-using-bash-automatically-without-the-interactive-editor – user68186 Jun 13 '22 at 22:50
  • 1
    All this maybe useless. Picard may not start for the user if the user is not logged in and the user's GUI settings are not initialized. You may want to start Picard automatically after user logs in. – user68186 Jun 13 '22 at 23:05

1 Answers1

0

The answer below does not yet run picard at the background, nor did I write unit tests for it. It does however automatically set up a crontab at reboot from a bash script.

run_tribler_at_boot() {
    local git_dir="$1"
# Create cronjob command
cron_command="@reboot $git_dir/tribler/src/./tribler.sh  > tribler.log"

# Check if crantab contains run at boot line already:
found_cron_command=$(crontab -l | grep "$cron_command")
if [ "$found_cron_command" == "" ]; then

    # Add cronjob to crontab if it is not in yet.
    write_cronjob "$git_dir" "tribler_cron" "$cron_command"
elif [ "$found_cron_command" == "$cron_command" ]; then
    echo "cronjob is already in crontab."
else
    echo "Error, the cronjob was not correctly in the crontab"
    echo "(most likely the cron_command:$cron_command is in in duplo):"
    echo ""
    crontab -l
    echo ""
    echo "Remove the (duplicate) cron_command(s) from the crontab -e"
    echo "and try again."
    exit 6
fi

}

write_cronjob() { local git_dir="$1" local temp_cronjob_filename="$2" local cron_command="$3"

# Write out the current crontab into a new file with filemname in var mycron.
crontab -l > $temp_cronjob_filename

#echo new cron into cron file through append.
echo "$cron_command" >> $temp_cronjob_filename

#install new cron file
crontab $temp_cronjob_filename


# TODO: Verify the temp_cronjob_filecontent equals the crontab -e output.
#rm $temp_cronjob_filename

}

And it can be called with:

run_tribler_at_boot() "~/git"

Assuming the application, in this case tribler, is stored into ~/git/tribler/src/tribler.sh To change to picard the cron_command can be set to:

cron_command="@reboot picard"

I did not yet figure out how to run it in the background.

a.t.
  • 317