39
Run command at login (piefed.blahaj.zone)

I just realized that you can run a command at login by appending it to your .bashrc

For instance, I added a curl pointing to a certain address that immediately upon login returns a message that tells me whether I'm connected to my VPN provider or not.

Sorry for the beginner revelation. Just thought it was cool. :3

you are viewing a single comment's thread
view the rest of the comments
[-] folekaule@lemmy.world 17 points 2 hours ago

That's a neat trick. Easy to forget if you're connected or not sometimes.

I wanted to add a little detail since it's easy to misunderstand "upon login" as "only at login". Actually, .bashrc runs every time you start a new interactive non-login shell, not just on login. For your use case it doesn't matter, but it may surprise someone if multiple copies of a program starts running as a result of adding it to .bashrc.

I have quoted the bash man page below:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, bash reads and exe‐ cutes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. The --norc option inhibits this behavior. The --rcfile file option causes bash to use file instead of ~/.bashrc.

[-] akunohana@piefed.blahaj.zone 5 points 2 hours ago

Thanks for the deets! :D

Right, if the program doesn't terminate/close automatically, or if it's one that runs over a longer period of time, then you might unknowingly and unnecessarily start several threads of it, whenever you spawn an interactive shell that is not a login shell.

this post was submitted on 04 Dec 2026
39 points (97.6% liked)

Linux

69006 readers
1179 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 7 years ago
MODERATORS