Page 1 of 2

Jessie: How to run a script AFTER boot is done?

Posted: Fri Oct 02, 2015 4:38 pm
by Bosse_B
I need to run a user level script to start VNC server directly after boot.
The old init.d scripting does not work anymore on Jessie, so I have to figure out a different way not involving a manual login via PuTTY to launch the server.
I do have a vnc.sh script in the pi home directory which if executed will start the VNC server.
It contains this:

Code: Select all

#!/bin/sh
vncserver :1 -geometry 1280x900 -depth 24 -dpi 96
It starts the server on port #5901 and after it has been run manually all is fine.
But I would rather have it done automatically upon boot....
Is there any way to detect that boot is done and launch it?

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 10:03 am
by Bosse_B
I reverted to Raspbian Wheezy 2015-05-05 and all is now working fine including VNC on boot.
I think Jessie is a bit too new for everything to work.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 11:40 am
by hippy
Bosse_B wrote:I think Jessie is a bit too new for everything to work.
I would say it's more that systemd is unfamiliar to most Pi users so they do not yet know how to do things 'the systemd way'. There will inevitably be some teething problems but I have every confidence the Raspberry team will do all they can to quickly resolve those.

I expect "How to run things after boot-up" will become a quite common question. I am currently trying to figure that out myself and it is more complicated than 'just add this line to this file'.

If /etc/rc.local is no longer the last thing run at boot-up, then it might be an idea for Rasbian to add an /etc/rc.booted ( or similar ) which is the last thing run. That way those unfamiliar with systemd can add commands and calls to shell scripts in there without having to worry about systemd per se. That might not be the best way to do things, but it would provide an easy to use, easier to explain solution.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 12:01 pm
by kusti8
The current workaround is to add a sleep 10 into rc.local so that it starts you program last. It's not elegant but it works.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 1:38 pm
by Joe Schmoe
It will work once you do the obvious step of uninstalling systemd.

I assume that the next release of Jessie will have that step done already for you.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 1:45 pm
by ShiftPlusOne
Joe Schmoe wrote:I assume that the next release of Jessie will have that step done already for you.
Absolutely not.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 2:09 pm
by Joe Schmoe
ShiftPlusOne wrote:
Joe Schmoe wrote:I assume that the next release of Jessie will have that step done already for you.
Absolutely not.
Eventually, it will. Maybe not in the next release.

In the meantime, we can expect a lot of threads like this one and others like it. I guess that'll keep us advice-givers busy.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 4:01 pm
by gkreidl
Joe Schmoe wrote:It will work once you do the obvious step of uninstalling systemd.

I assume that the next release of Jessie will have that step done already for you.
You've already been part of a flame war against systemd in the past and now you're starting the same rubbish again. You should change your signature to "I'm only here to make trouble". That would be honest at least.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 4:49 pm
by DougieLawson
Joe Schmoe wrote:
ShiftPlusOne wrote:
Joe Schmoe wrote:I assume that the next release of Jessie will have that step done already for you.
Absolutely not.
Eventually, it will. Maybe not in the next release.

In the meantime, we can expect a lot of threads like this one and others like it. I guess that'll keep us advice-givers busy.
You're wrong!

I've had two RPis running Jessie for months and had no end of problems. During this week I migrated my most important machine to Jessie (first one to get rebooted when I got home last night). Yesterday/this morning I've migrated another three machines. I've left my machine running with a Wolfson sound card on Raspbian and one other which is running an old copy of NOOBS (those two haven't beem booted for 118 days) that wasn't built by me.

I have to say I think the Raspberry Pi Foundation folks who've spent the last six months working on getting Jessie ready for the general population have done a superb job and there's no way I'm going to rip systemd out. It's going to take a while to learn it but after all the migration work it appears to be much more stable than sysvinit.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 4:56 pm
by Joe Schmoe
So, you're saying that systemd used to be a pain in the neck (your words: "No end of problems"), but now you've learned enough about its quirks that you can tolerate it.

Good for you. I am impressed.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 5:17 pm
by Bosse_B
DougieLawson wrote: I've had two RPis running Jessie for months and had no end of problems.
....
I have to say I think the Raspberry Pi Foundation folks who've spent the last six months working on getting Jessie ready for the general population have done a superb job and there's no way I'm going to rip systemd out. It's going to take a while to learn it but after all the migration work it appears to be much more stable than sysvinit.
I am a bit confused by your statement that you have had no end of problems with the Pi:s running Jessie and yet you compliment the Foundation crew for Jessie...
Is this actually what you mean? (Lots of problems, but it is good).

I (thread starter) have only limited exposure to Jessie but my first impressions were good. Very fast boot and initially booting to the desktop rather than to RaspiConfig is also a boon, even though I got confused since I am accustomed to the RaspiConfig for all Pi:s I built so far.

Unfortunately I had to abandon Jessie due to two issues:
1) Inability to start VNC service during boot
2) Could not install Free-Pascal on Jessie (no apt-get, I tried to do it from sources)

Backed off to Wheezy and both tasks accomplished without a glitch.

But I guess Jessie is the future anyway, so it means that "someone" will iron out the wrinkles and then it will be a good distribution.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 5:39 pm
by DougieLawson
I've been running Jessie for 18months (and more). It's been unstable at times, so much so that when I built my B+ on Jessie it had to be reverted to Wheezy before it went online 24/7 on the internet.

The last six months hard work has fixed all of the problems I've had with Jessie. The last bug being one with NFS not being unmounted before the network interface was disabled which caused reboots to hang.

The systemd detractors need to shut up and stop telling folks to rip systemd out of their systems. Because of the hard work by folks including Plugwash and ShiftPlusOne who've built a new Raspbian we have a good OS that is ready to release to ALL Raspberry Pi users.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 5:58 pm
by Joe Schmoe
IOW, Dougie used to be a rational guy, but now he's drunk the Kool Aid.

That's the takeaway of his recent posts.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 6:43 pm
by rpdom
Although I have so far removed systemd from most of my Jessie systems, I can see that it might have some advantages if configured properly. Therefore I will work on learning how to tame the beast and see how many of my systems I can get converted over.

It won't be all of them. I've tried moving my main laptop to jessie on a few occasions and it is unusable :(

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 6:51 pm
by jbernardo
DougieLawson wrote:The systemd detractors need to shut up and stop telling folks to rip systemd out of their systems.
And unfortunately, this is why I stopped trying to have a rational argument with systemd defenders. It always ends like this.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 6:56 pm
by Joe Schmoe
jbernardo wrote:
DougieLawson wrote:The systemd detractors need to shut up and stop telling folks to rip systemd out of their systems.
And unfortunately, this is why I stopped trying to have a rational argument with systemd defenders. It always ends like this.
Indeed.

It always boils down to:

Sit down, shut up.
Get on the bus.
Drink the Kool Aid.

Never any defense for it (because there is none) - always "It's the future, get used to it."

The point is: If we wanted to be with the crowd (which, don't get me wrong, has advantages for many people/users - it is the way to go if your goal is to make money. As an old boss told me (in all seriousness), "Don't buck trends"), we'd stick with Windows.

We like Linux because it allows us to do our own thing. To do things based on merit and personal choice, not on crowd-psych.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 7:00 pm
by DougieLawson
If it isn't the future why has every major Linux distro dropped sysvinit and adopted systemd rather than upstart?

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 7:05 pm
by Joe Schmoe
DougieLawson wrote:If it isn't the future why has every major Linux distro dropped sysvinit and adopted systemd rather than upstart?
I don't deny that it is the future. I just reject the notion that anyone (i.e., anyone lacking the crowd mentality) should accept that as a reason to use it. As I said, if we had that mentality, we'd stick with Windows.

Essentially, I am saying that Linux-think is being molded by TPTB to resemble Windows-think, and I don't like it.

I also hated upstart, BTW. Not sure why you mentioned that.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 7:48 pm
by mahjongg
Joe Schmoe, please tone it down a bit.

As for systemd dislikers, in my humble opinion you dislike it just because you don't understand it, so are scared of it.
It seems however to bring much needed progress, so please leave your dislike at home...
Rather than complain about bugs and issues, please help resolve them.
You will never be able to revert the trend to using systemd, so better get along with it.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 7:57 pm
by Joe Schmoe
mahjongg wrote:J
You will never be able to revert the trend to using systemd, so better get along with it.
IOW,
sit down shut up get on the bus drink the kool aid

I personally will never use systemd, and I won't stop advising others to do likewise.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sat Oct 03, 2015 8:16 pm
by mahjongg
sit down shut up get on the bus drink the kool aid
didn't ask you that, just to be a bit calmer and more rational, and not to rant about your dislikes so much, and not be so offensive toward other posters here.
IOW "be nice". :P

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sun Oct 04, 2015 4:15 pm
by ShiftPlusOne
Cleared the thread of junk irrelevant to the OP's post.

If you want to argue about systemd, start your own thread. That's perfectly fine and allowed. Just be respectful if you do.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sun Oct 04, 2015 6:02 pm
by hippy
Having accepted that systemd is here, and accepting things have to be made to work with it, I decided to try and figure it out for myself.

I have got some way there which may help others trying to do the same, but I would appreciate some help and feedback from those more experienced with Linux and particularly with systemd experience.

This was the process I used. Files might not be in preferred places but it does at least work. I used upper-case just so things would stand out more amongst the systemd messages.

The first step was to get something to simply run under systemd which can be tweaked later to work as desired.

Create a shell script to run after boot-up. This will just output the date and time, put the same in a text file, and output a message to show that it is running ...

Code: Select all

sudo mkdir /etc/booted
sudo nano /etc/booted/booted.sh
  #!/bin/sh -e
  date > /booted.txt
  date
  echo "\n\nINSIDE BOOTED.SH\n\n"
  exit 0
sudo chmod +x booted.sh
We also need to create a unit file so systemd can do its thing. We do not particularly care where in the process things are run, only that they are, so "After=syslog.target" and "WantedBy=multi-user.target" are just 'best guesses' for now ...

Code: Select all

sudo nano /etc/systemd/system/booted.service
  [Unit]
  Description=SCRIPT TO RUN AFTER BOOTING
  After=syslog.target
  [Service]
  ExecStart=/etc/booted/booted.sh
  Type=oneshot
  [Install]
  WantedBy=multi-user.target  
Now we have to let systemd know about the added booted.service, and we need to enable that to be run when booted. And then we can reboot to see what happens. Note it's systemctl, C-T-L not systemctrl ...

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable booted.service
sudo reboot
And in the midst of all the boot-up messages we see "Starting SCRIPT TO RUN AFTER BOOTING..." and "[OK] Started SCRIPT TO RUN AFTER BOOTING",but no date and no echoed "INSIDE BOOTED.SH" which should be shown when the booted.sh script runs.[SOLVED]

Logging in and /booted.txt has been created and is as expected which shows booted.sh did run, and "sudo systemctl -l booted.service" shows ...

Code: Select all

booted.service - SCRIPT TO RUN AFTER BOOTING
   Loaded: loaded (/etc/systemd/system/booted.service; enabled)
   Active: inactive (dead) since Sun 2015-10-04 18:32:37 BST; 6min ago
  Process: 482 ExecStart=/etc/booted/booted.sh (code=exited, status=0/SUCCESS)
 Main PID: 482 (code=exited, status=0/SUCCESS)

Oct 04 18:32:34 raspberrypi booted.sh[482]: Sun  4 Oct 18:32:34 BST 2015
Oct 04 18:32:34 raspberrypi booted.sh[482]: INSIDE BOOTED.SH
Oct 04 18:32:37 raspberrypi systemd[1]: Started SCRIPT TO RUN AFTER BOOTING.
So the date and echoed messages are shown by that but they never made it to the TV screen systemd listed messages.

Anyone know what I need to do to make that happen ?
[SOLVED]

Also, if anyone knows what [Unit]Before=, [Unit]After= and [Install]WantedBy= settings should be to have something executing when systemd completes that would save me some researching and experimentation.

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sun Oct 04, 2015 6:36 pm
by hippy
To get echoed output to the TV screen at bootup you need to play with the [Service]StandardOutput= and [Service]StandardError= entries. Not sure they are entirely right but this gets the output on screen ...

Code: Select all

sudo nano /etc/systemd/system/booted.service
  [Unit]
  Description=SCRIPT TO RUN AFTER BOOTING
  After=syslog.target
  [Service]
  ExecStart=/etc/booted/booted.sh
  Type=oneshot
  StandardOutput=syslog+console
  StandardError=syslog+console
  [Install]
  WantedBy=multi-user.target  

Re: Jessie: How to run a script AFTER boot is done?

Posted: Sun Oct 04, 2015 6:52 pm
by ShiftPlusOne
hippy wrote:Not sure they are entirely right but this gets the output on screen
The way we've done it for rc.local is to add an override file with the following content:

Code: Select all

[Service]
StandardOutput=tty
If you add that to your booted.service, that should do it too.