I've learned how to run commands/applications automatically when I login

I use OneDrive in both Windows and GNU/Linux. To access/manage my files on OneDrive in GNU/Linux, I use a client I found on GitHub at onedrive-abraunegg. My distribution of choice (Solus) keeps the current version in the repository. Some distributions don’t do that, so if you want/decide to use it, check the docs/INSTALL.md page (linked in the onedrive-abraunegg landing page) to see if yours does. Setting up and using the client is covered in the docs/USAGE.md page, so I won’t cover all that here.

I dual-boot Solus GNU/Linux with Windows 11, and I switch between them a lot. I have been searching for a way to synchronize the files in my local OneDrive directory with OneDrive on the Internet when I log in. The onedrive-abraunegg client has a command to do that (onedrive --synchronize). I want to run that command when I log in as a regular user (not as an administrator). If you have one or more commands/applications you want to start/run when you log in, what follows may help.

First, I created a bash script (startup.sh) as follows:

#!/bin/bash
# Synchronize OneDrive at system start
onedrive --synchronize
echo "OneDrive synchronized: "$(date)>> ~/.config/onedrive/onedrive.log
echo >> ~/.config/onedrive/onedrive.log
exit
  1. Tell the system this is a bash shell script.
    
  2. Describe the intent of the following command.
    
  3. Execute the 'onedrive --synchronize' command.
    
  4. Output a text string containing the current date and time to a log file.
    
  5. Exit
    

Second, I create a desktop launcher (Run startup commands.desktop) in ‘~/.config/autostart’ using the Dolphin file manager (I’m using KDE’s Plasma desktop environment).

  • Navigate to ‘~/.config/autostart’ in Dolphin.
  • ALT+Click an empty area in the directory’s main pane and select ‘Create New > Link to Application’.
  • Give the desktop launcher a name in the top field (where it says ‘Link to application’ now).
  • Switch to the ‘Application’ tab.
  • Enter the same name in the ‘Name’ field.
  • Optionally, you can enter a Description, Comment, and/or Environment Variables if needed/wanted.
  • For the ‘Program’ field, I click ‘Browse’ and navigate to my ‘srartup.sh’ script, and select it.
  • Click the ‘OK’ button to create the launcher.

Now, when I restart my computer to log into Solus, OneDrive is automatically synchronized for me, and a line containing the date and time of the synchronization is added to the ‘~/.config/onedrive/onedrive.log’ file.

You can create desktop launchers in the same way/location to start any application or run any command when you log in. I suggest you use a script file with a desktop launcher as I did to execute bash/terminal commands at startup, but create separate desktop launchers to run applications so you have more granular control over them.

UPDATE!!

I copied my Scripts directory and my ‘Run startup commands.desktop’ desktop launcher file from my primary laptop PC to OneDrive. I was able to use them on my desktop computer, which also dual-boots Windows 11 and Solus Linux with the Plasma DE. Even better, they also work as expected on my older Dell laptop PC, which dual-boots Windows 10 and Manjaro Linux with the xfce4 DE.

The only extra step needed to make everything work as expected (in both cases) was to run ‘chmod +x statartup.sh’ from the Scripts directory after copying everything to each computer. It’s my guess that this setting is stripped from the file when it’s stored on OneDrive (or possibly during the copy-paste operation?), or it’s not stored in the file itself, but in the file system.

Does anyone know how these permissions settings work under the hood/why they wouldn’t transfer from system to system as part of the file?

UPDATE January 13, 2024:

I discovered that my script runs at login, but the onedrive client isn’t synchronizing my local OneDrive file store with OneDrive in the cloud. After giving the issue some thought, I realized that my Internet connection doesn’t connect until I complete my login, so I inserted this command sleep 10 on the line before the onedrive --synchronize command to make my script wait ten seconds before running the synchronization command, and my problem is solved. Interestingly enough, I don’t appear to need the sleep command in my new Solus-xfce system on my older Dell laptop computer. This makes me wonder what’s different between the two editions (xfce and Plasma) autostart implementation, or if it’s because my Dell laptop is slower than my desktop and primary laptop computers.

I hope others find this useful,

Ernie

5 Likes

Just one addition as my 2 cents;
What you describe in your post is to launch a program upon your login.
This is not on system start, as the title says.
For that you would need to create a service, that can run really on system start (even without anyone logging in).
Of course you don’t want to run onedrive client on system start, it’s required to run when you log in, so you did it right! :wink:
I just wanted to make it clear, that system start is not equal to a users login.

5 Likes

Hi @ernie ,
That is a fine way of doing it, provided you note what @kovacslt
said

system start is not equal to a users login

There is not much difference if you are the only user.

It is also possible to use cron to start a process at boot time -
ie at what Laszlo calls startup.
Here is an example

Setting up crontab to do a mount on reboot

The following steps are needed in the guest system

    setup a virtiofs mount as in reply No 3.
    make sure the cron daemon is running
    make a script file to do the mount

#! /bin/bash
mount -t virtiofs mount-tag /mnt/mymount

call the file mountscript.sh

    put the script file somewhere like ~/bin

    make the script file executable

    as superuser, edit the system-wide crontab file /etc/crontab with any text editor, and add the line

@reboot root /path/to/mountscript.sh

    Reboot and it should mount automatically

I copied that out of a topic by Rosika and myself on virt-manager.

Regards
Neville

2 Likes

Thank you for your observation. I corrected the title of this item. I’m sorry for any confusion. slight_smile:

Ernie

2 Likes

See my reply to @kovacslt. I’ve corrected the title of this item. Again, I regret any confusion it created.

Since I’m the only user on my computers, for me, login and startup are synonymous events. When the system starts, I log in. If I had multiple users, and I had anything I wanted to run for all of them, the crontab solution would be the right way to go. For my case or any individual user case, what I describe in this item may offer the best solution because it works for a single user and doesn’t affect any other users on the computer.

What do you think?

Ernie

5 Likes

No problem Ernie, I hope I was not hurting you. Pleas excuse me if I did it unintentionally.

This was not the first time I see mixing these startup things up, so it seems to me a bit common.

Yes, that solution is the right one for you, and I would do it the same way, because this program is intended to run after login. In a multiuser environment, such as my family, I’d create the same autostart for every single user who needs this, as all those users need to run that program in their own security context after they logged in, and it expands the functionality of their own desktop experience in a way…

So the right answer, I think, is that first decide what purpose that program is needed for, and launch it upon the right event.

If it serves a single local user who is locally logged in, then running that program with the autostart is the way to go.
For example, typically Gnome-keyring is such a thing.

If that program is a systemwide service, and should work independently of local users logged in or not logged in, then launching it at system start (boot up) is the way to go.
For example openssh server is such a thing.

3 Likes

Hi Ernie,
I dont think it was confusing.
I use the startup facility in Xfce… it is like your KDE… it happens at login
cron is only one way to do a startup at boot.
There is the old fashioned way with rc scripts, and there is
services, as Laszlo suggested.
Regards
Neville

2 Likes

You weren’t. I appreciate it when I’m informed about any errors in what I write/post. It is my intention to avoid using arcane technical terms when I can. I want what I write to be as easy to understand for those new to GNU/Linux as possible, while remaining accurate and correct for those who are GNU/Linux Gurus, and everyone in between. If you find any errors in anything I write, please tell me. I do proofread what I write before posting, but I’m no professional writer, and I can easily miss my errors.

Thank you for your input,

Ernie

4 Likes

There are not many people who will even attempt to write a topic. It is not an easy task. Keep at it.

2 Likes

I absolutely will.

I don’t consider myself to be anything close to a GNU/Linux expert, but I’m learning, and I have been (off and on) since the late 1990s. Furthermore, I’m comfortable in the terminal, probably because I started out using a Commodore Vic 20, then a Commodore 64, then an IBM-compatible PC with 64KB RAM and a 100MB hard drive, powered by MS-DOS 3.1 (and later) up to 5.x. I enjoy working with the power, speed, and immediacy of the bash interpreter, and all the things I can accomplish with it, even though I use the graphical file managers and text editors too. I suppose you could say I use what I consider to be the best tool for whatever job I’m trying to get done.

If I accomplish anything with all my posts, I hope they make life easier for those who follow,

Ernie

3 Likes

I remember that.
Did you use CP/M?
Have you done any programming?

Maybe, but I mostly used the basic interpreter to enter the code for games, so I could play them. The Vic 20 didn’t have any storage, no floppy or disk drives, so when I started it up, I had to enter code from the user’s manual to do anything. The '64 was a lot better because it had a floppy drive, so I could save anything I wrote to disk for later use.

It has been so long that if I did use CP/M at all, I don’t remember anything about it now.

I did a bit of coding from time to time. I learned a little about Assembly Language on the IBM-compatible PC, and used it to write little .com utilities to make DOS do new things. By the time that computer bit the dust, I had three versions of the BASIC interpreter installed, and programs written for each one. I was particularly proud of a utility I wrote to choose the correct interpreter for whichever program I wanted to run. IIRC, I added an identifying string to each program as a comment, so the interpreter would ignore it, but my utility would read the first line of the script and execute the correct interpreter with the script. IIRC, I named it ch-int (for choose interpreter), and the command to run a program was ‘ch-int [program name]’.

After I started using Windows, I learned a bit about a wide variety of programming languages, from C to Kotlin. I never did much with any of them. Recently, I’ve been trying to learn more about bash because its command set closely resembles a fully functional programming language. I’d like to learn more about it.

That about covers me and programming,

Ernie

Abhishek’s bash tutorial is a good starting point.
You have more programming experience than most
people. It shows in the way you go about doing things.

1 Like

Perhaps, but I don’t consider myself a programmer. I think of myself as more of a programming/Computer Science student, if anything at all. I can figure out how to develop a function that takes specific input(s), then processes them to generate the expected output. When it comes to developing anything that’s really complex, I’m a bit out of my league :slight_smile:. With that said, I’ll agree that studying programming and other Computer Science topics has had an effect on how I do things. Hopefully, my work turns out to be well-ordered, if a bit iterative in the development process.

Ernie

3 Likes

After some thought, I think I may have found the answer to my own question. OneDrive is intended/created as an extension to Microsoft’s Windows operating system and for use with their NTFS file system. They have no reason to support features of file systems other than their own. Does this sound right to everyone else, or am I missing something?

Ernie

That sounds right.
If you copy files to an ntfs system, you lose permissions
and links, because it does not support them.
I read somewhere microsoft are making a move to add these features to ntfs. Dont know when .

That would be a good move on their part. On the other hand, maybe they should base their whole OS on the Linux kernel. Then they’d have something really secure. I’m not sure about how I feel about such an idea, though. It would make Windows more secure from the get-go, but what effect would such a move have on Open Source? I can think of several advantages for GNU/Linux, and some darker outcomes too. It’s the dark side that always concerns me.

Ernie

Filesystems are part of the kernel. They would have to drop ntfs to go to a linux kernel.
A linux or BSD kernel with a windows DE would be a popular system.
Some Linuxes are quite close to that already.

Either that, or they would have to add native NTFS support to the Linux kernel. After all, they know everything about how their system works with the Windows kernel. If anyone could make the Linux kernel able to support NTFS, and even Windows system calls, they should be the ones who can. Then again, we both know that’ll never happen :slight_smile:.

Ernie

No good, Linux needs a filesystem with links and permissions.
They have to fix NTFS, or scrap it.