Running InSync on startup

I’ve been using InSync for quite a while now - it’s basically a google drive client with better options and multiple account support - I’m using it to synchronize content (tv shows, movies, pictures) from my Windows 10 HTPC into the OSMC Raspberry Pi, which serves as an HTPC on-the-go extension. Very useful for traveling, hotel rooms, etc…

For whoever’s interested, the raspberry pi version of InSync is available here:

My problem: I’m trying to have InSync start on startup of the raspberry pi.
I’ve following this guide:

Specifically, what I did was:

cd /home/osmc
mkdir scripts
cd scripts
touch bootlog.log
nano bootlog.sh
chmod +x bootlog.sh

The content of bootlog.sh is now:

#!/bin/bash
now=$(date +“%Y %b %d %H:%M:%S”)
echo ^ ^ Booted: $now ^ ^ >> /home/osmc/scripts/bootlog.log
cd /home/osmc/insync-portable
pwd >> /home/osmc/scripts/bootlog.log
./insync-portable start
echo insync started >> /home/osmc/scripts/bootlog.log
exit

And then:

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

[Unit]
Description = A systemboot log
After = mediacenter.service
[Service]
Type = single
ExecStart = /home/osmc/scripts/bootlog.sh
[Install]
WantedBy = multi-user.target

sudo chmod 755 /etc/systemd/system/insync.service
sudo systemctl daemon-reload
sudo systemctl enable insync.service
sudo systemctl start insync

The problem:
The log indicates that something happened…:

^ ^ Booted: 2017 Apr 12 09:04:18 ^ ^
/home/osmc/insync-portable
insync started

But insync is not running. Not with sudo systemctl start insync and not if I reboot the RPi.
If I manually do:

cd /home/osmc/insync-portable
./insync-portable start

This works fine. So for some reason it is not executed the way it’s supposed to.
This is especially frustrating, because I had it working properly for a very long time, recently had to do a fresh OSMC install because of an SD card problem, and now I don’t remeber how I set it up the first time…

Any assistance will be highly appreciated (apologies for the long post, I tried to include as many details as possible to assist with solving this problem). Thanks!

Hi. Good write up with lots of information. Just what we like (and need!).

A few points stand out:

  1. You need to create your systemd .service file in /lib/systemd/system rather than in /etc/systemd/system. When you run systemctl enable it then creates a symbolic link in /etc/systemd/system/multi.user.target.wants back to the real file.

  2. Your shell script should ideally send (stout and stderr) output to the log file when it runs ./insync-portable.start. Simply append >>/home/osmc/scripts/bootlog.log 2>&1 to the line.

The systemd .service file might need a bit of finessing but let’s see how it works first.

Thanks!
I almost figured it out…
So I did both your suggestions, and now in the log I see a line which says:

To add accounts in headless mode, use the “insync-portable add_account” command. Run “insync-portable help add_account” for more details.

Which means that inSync thinks it’s not set up with my accounts (even though it is). So then I realized that’s going on.
If I do, from the terminal:
./insync-portable start
that works fine.

However, if I do:
sudo ./insync-portable start
then I get this error message from above.

So bottom line, I need to either set up insync again as sudo or have the bootlog.sh script run this command as the regular osmc user (an option which I prefer, because I already have insync all set up using non-sudo commands). Which route should I go?

You can add a line in the [Service] section of the .service file User=osmc. That’s a pretty minimal change and might just work

Unfortunately, this did not work. I added user=osmc, reboot, got the same error as if insync is trying to run as a different user. Then I decided to give up, removed the previous setup, re-setup this time using sudo.
And now I get the same error again… as if systemd is running a different user than the one used to set up InSync…

I have no idea what to do now…
This is so upsetting, if only I remembered what I did to set everything up last time…

First try User=osmc (upper-case U). It will run as root unless told otherwise.

And since you are indeed running it in headless mode, have you followed the instruction to run insync-portable add_account?

I have set it up, of course. I believe the previous problem was with the lower case u in User. However, this still does not work. So to sum everything up so far:

I have done:
sudo nano /lib/systemd/system/insync.service

The content of which is now:

[Unit]
Description = A systemboot log
After = mediacenter.service
[Service]
User = osmc
Type = single
ExecStart = /home/osmc/scripts/bootlog.sh
[Install]
WantedBy = multi-user.target

(capital U in User).
Then I run these commands:
sudo chmod 755 /lib/systemd/system/insync.service
sudo systemctl daemon-reload
sudo systemctl enable insync.service
sudo systemctl start insync

Then, to create bootlog.sh:
cd /home/osmc
mkdir scripts
cd scripts
touch bootlog.log
nano bootlog.sh
chmod +x bootlog.sh

The content of which is now:

#!/bin/bash
now=$(date +“%Y %b %d %H:%M:%S”)
echo ^ ^ Booted: $now ^ ^ >> /home/osmc/scripts/bootlog.log
cd /home/osmc/insync-portable
pwd >> /home/osmc/scripts/bootlog.log
./insync-portable start >> /home/osmc/scripts/bootlog.log 2>&1
whoami >> /home/osmc/scripts/bootlog.log
echo insync started >> /home/osmc/scripts/bootlog.log
exit

And, of course, I do:
./insync-portable start
./insync-portable add_account -a [my-account-id]
./insync-portable get_account_information

Last line shows me that everything is fine. Then:
sudo reboot

After boot, the log file shows:

^ ^ Booted: 2017 Apr 12 11:44:09 ^ ^
/home/osmc/insync-portable
osmc
insync started

So no error message. When insync succeeds, there’s no OK message, so that seemed like good news.

Then:
./insync-portable get_status
And I get the message:

Insync doesn’t seem to be running. Start it first.

Which is a message that indicates that ./insync-portable start has not been run…

Update:

I added this line to the bootlog.sh file:
./insync-portable get_account_information >> /home/osmc/scripts/bootlog.log

And the result log is now:

^ ^ Booted: 2017 Apr 12 11:53:09 ^ ^
/home/osmc/insync-portable
osmc
Insync doesn’t seem to be running. Start it first.

insync started

So it’s failing to start but not reporting, or starting as a different user still.

Is there a different method other than systemd?

Add 2>&1 to the end of that line and any other log lines so you see any error as well as any output.

Once it says it has started can you see it with ps aux | grep insync? That will tell you if it is running as a different user.

If it is still running as a different user you may have to add sudo -u osmc to the start of the start command.

I suggest we stay with systemd for now. No pain, no gain. :wink:

  1. In your .service file Type = single should be Type = simple, though that might not actually be the correct type to use. It depends what happens later in the insync-portable script. The other type worth trying is Type = forking.

  2. Give the full output of all the 3 systemctl commands you listed above. Then run systemctl status insync.

  3. It’s probably a formatting issue but the shell script should begin #!/bin/bash.

Works!
Type = forking was what did the trick.

Thank you so much for your support!

Excellent news. Don’t forget to mark as solved.

@sam_nazarko OP was following Running scripts on startup and shutdown and it contains an error: systemd Type = single should be changed to Type = simple .

One example also contains EnvironmentFile=-/etc/sysconfig/network but the description refers to Enviroment (NB spelling).

But nice guide.

Edit: It seems like the guide is suggesting that the .service file should be created in /etc/systemd/system whereas the best location is /lib/systemd/system. That way systemctl enable/disable will create/remove a symbolic link in /etc/systemd/system back to the real file.

Suggested changes have been made, thanks for pointing them out.

  1. Type = single not changed.

  2. Still a reference to sudo chmod 755 /etc/systemd/system below the example test.service file.

  3. Spelling of Environment has been corrected but the actual example uses EnvironmentFile.