2

I'm learning bash scripting and I just made a short-project to notify me (with notify-send) when my battery is low or charged.

I'd like some suggestions of how can I run this script without showing the terminal. My idea is to add it in "Startup Applications" software to run when I boot the system, but I don't want to show the terminal, just to run in the background.

1

You could setup a systemd service.

You could create a file (as sudo) with you favorite editor (nano, vim etc) like:

sudo nano /etc/systemd/system/myexecuteable.service

It should contain at least the following:

[Unit]
Description=start my executable after multi-user target

[Service]
Type=simple
ExecStart=/path/to/my/executable

[Install]
WantedBy=multi-user.target

To ensure that the service will start after reboot execute: sudo systemctl enable myexecutable.service

That service will run continously if you bash has some kind of loop. If that executable has some output (use "logger for that in a bash file) you can check it any time using journalctl -u myexecutable.service

3
  • That won't work with notify-send. At least not without some fiddling around (see answers here on unix.SE relating to notify-send and cron) – roaima Oct 24 '20 at 23:11
  • Right, You'll have to incorporate XAuthority and a user in the bash. – kanehekili Oct 24 '20 at 23:54
  • I'll try it, first, if I can't make it functional, I'll try cron. Thank you for answering. – pedrohgodoys Nov 2 '20 at 4:16
1

A script may be run in the background from the command line like this:

$ ./myscript.sh &

In other words, append & to the command.

You've said you want to run this script "without showing the termial". I think there are two ways to do this:

  1. create a systemd service - as in this answer

  2. start the script as a cron job when the system boots:

To set up a cron job, you will need to create a new crontab entry.

To do this, open a terminal, and type the following command:

$ crontab -e

This will open your crontab in an editor (nano is a good choice for editor). When you are in the editor, add the following line to the bottom of the file:

@reboot /home/user/myscript.sh & >> /home/user/myscript.log 2>&1 

Where /home/user/myscript.sh is the full path to your script file. Any output from your script will be redirected to the file at /home/user/myscript.log. Be sure to check this logfile for any error messages.

Save the file ( ^ o ) if using nano, and exit the editor ( ^ x).

Some things to remember:

  1. Make sure myscript.sh is executable.

  2. If you use any commands in myscript.sh, make sure the system knows where to find them - i.e. use a full path to the command, or declare a PATH in your script.

Let us know if you have further questions (post a comment) & we'll try to help.

1
  • Thank you for answering.I'll try first the systemd service. If I cant manage to do this, I'l try cron. – pedrohgodoys Nov 2 '20 at 4:14

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.