Suppose now the Lagrangian $L$ is a function of $y(x), y'(x)$ and also $y''(x)$, i.e. it contains second derivatives w/r to the parameter $x$.
It is straightforward to adapt the
usual procedure to this case: write
\begin{align}
Y(x,\epsilon)=y(x)+\epsilon\,\eta(x)
\end{align}
for an otherwise arbitrary function $\eta$.
We then have the parametrized integral
\begin{align}
I(\epsilon)=\displaystyle
\int_a^b\,dx\,L(x,Y(x,\epsilon),Y'(x,\epsilon),
Y''(x,\epsilon))
\end{align}
and we want to find $L$ at $\epsilon=0$
so that
\begin{align}
0&=\frac{dI}{d\epsilon}\vert_{\epsilon=0}\, ,\\
&=\displaystyle\int_a^b\,dx\,
\left(
\frac{\partial L}{\partial Y}
\frac{\partial Y}{\partial \epsilon}+
\frac{\partial L}{\partial Y'}\frac{\partial Y'}{\partial \epsilon}
+\frac{\partial L}{\partial Y''}\frac{\partial Y''}{\partial \epsilon}
\right)\vert_{\epsilon=0}\, ,\\
&=\displaystyle\int_a^b\,dx\,
\left(
\frac{\partial L}{\partial y}\eta
+\frac{\partial L}{\partial y'}\eta'
+\frac{\partial L}{\partial y''}
\eta'' \right)\, .
\end{align}
We need to turn around the terms in
$\eta'$ and $\eta''$. A first integration
by parts will do this:
\begin{align}
\displaystyle\int_a^b\,dx\,
\frac{\partial F}{\partial y'}\frac{d\eta}{dx}
&=\frac{\partial F}{\partial y'}\eta(x)\Bigl\vert_a^b-
\displaystyle\int_a^b\,dx\,\eta
\frac{d}{dx}\left(\frac{\partial F}{\partial y'}\right)\, ,\\
\displaystyle\int_a^b\,dx\,
\frac{\partial F}{\partial y''}
\frac{d\eta'}{dx}&=
\frac{\partial F}{\partial y''}\eta'(x)
\Bigl\vert_a^b-
\displaystyle\int_a^b\,dx\,\eta'
\frac{d}{dx}\left(\frac{\partial F}{\partial y''}\right)\, .
\end{align}
We assume now that the function $\eta$ is
chosen so that $\eta(b)=\eta(a)=0$ as before.
In addition, we must also assume
that $\eta'(b)=\eta'(a)=0$, a new condition.
In this way we have
\begin{align}
\displaystyle\int_a^b\,dx\,
\frac{\partial L}{\partial y'}\eta'&=
-\displaystyle\int_a^b\,dx\,\eta\,
\frac{d}{dx}
\left(\frac{\partial L}{\partial y'}\right)\, ,\\
\displaystyle\int_a^b\,dx\,
\frac{\partial L}{\partial y''}\eta''&=
-\displaystyle\int_a^b\,dx\,\eta'\,
\frac{d}{dx}\left( \frac{\partial L}{\partial y''}\right)\, .
\end{align}
We still need to turn $\eta'$ around one last time. Using integration by parts again:
\begin{align}
-\displaystyle\int_a^b\,dx\,\eta'
\frac{d}{dx}\left(\frac{\partial F}{\partial y''}\right)= + \displaystyle\int_a^b\,dx\,
\eta\,\frac{d^2}{dx^2}\left(\frac{\partial F}{\partial y''}\right)
\end{align}
where the boundary condition $\eta(b)=\eta(a)$ has been used to eliminate the boundary term.
Thus, putting all this together, we get
\begin{align}
0=\frac{dI}{d\epsilon}\Bigl\vert_{\epsilon=0}
=\displaystyle\int_a^b \,dx\,\eta\,
\left(\frac{d^2}{dx^2}\left(\frac{\partial F}{\partial y''}\right)-\frac{d}{dx}
\left(\frac{\partial F}{\partial y'}\right)
+\frac{\partial F}{\partial y}\right)\, .
\end{align}
Since $\eta$ is arbitrary (up to the boundary conditions), we find therefore the function
$L$ must satisfy the differential equation
\begin{align}
0=\frac{d^2}{dx^2}\left(
\frac{\partial L}{\partial y''}\right)-
\frac{d}{dx}\left(\frac{\partial L}{\partial y'}\right)+\frac{\partial L}{\partial y}\, .
\end{align}
The generalization to $L$ containing yet
more derivatives is obvious: for
a the derivative of order $k$ we obtain a
sign $(-1)^k$ as we need $k$ integrations
by parts. Thus, we obtain the generalized
Euler-Lagrange equation in the form
\begin{align}
0&=\sum_{k}(-1)^k\frac{d^k}{dx^k}
\left(\frac{\partial L}{\partial y^k}\right)
\equiv E(L)\, ,\\
E&=\sum_k (-1)^k \frac{d^k}{dx^k}
\frac{\partial }{\partial y^k}\, .
\end{align}
There's some discussion of this (including how to properly define a conjugate momentum) in the following:
Riahi, F. "On Lagrangians with higher order derivatives." American Journal of Physics 40.3 (1972): 386-390.
and also in
Borneas, M. "On a generalization of the Lagrange Function." American Journal of Physics 27.4 (1959): 265-267.
Best Answer
You have to impose that $\eta(t_0)=\eta(t_1)=\dot{\eta}(t_0)=\dot{\eta}(t_1)=0$ where $t_0$ and $t_1$ are the endpoints of the time interval over which you are integrating. Then, the last term is: \begin{equation} \int_{t_0}^{t_1}\frac{d^2}{dt^2} \left(\frac{\partial L}{\partial\ddot{q}}\eta\right)dt = \left[\frac{d}{dt}\left(\frac{\partial L}{\partial\ddot{q}}\eta\right)\right]_{t_0}^{t_1} = \left[\eta\frac{d}{dt}\left(\frac{\partial L}{\partial\ddot{q}}\right)\right]_{t_0}^{t_1}+ \left[\dot{\eta}\frac{\partial L}{\partial\ddot{q}}\right]_{t_0}^{t_1} = 0 \end{equation} The Euler-Lagrange equation is then: \begin{equation} \frac{\partial L}{\partial q} - \frac{d}{dt}\left(\frac{\partial L}{\partial \dot{q}}\right) + \frac{d^2}{dt^2}\left(\frac{\partial L}{\partial \ddot{q}}\right) = 0 \end{equation} As a justification for the conditions over $\eta$ and its derivative at the endpoints observe that, in general, $\partial L/\partial\ddot{q}$ may depend on $\ddot{q}$, so the equation of motion will be of fourth order. To obtain a solution, four conditions will be needed. In the case of $L$ depending only on $q$ and $\dot{q}$, for a second order equation we needed two conditions: fixing $q(t_0)$ and $q(t_1)$. In the fourth order case, it is reasonable to fix $q(t_0)$, $q(t_1)$, $\dot{q}(t_0)$ and $\dot{q}(t_1)$.
Therefore, as $\delta q=\epsilon\eta$ and $\delta \dot{q}=\epsilon\dot{\eta}$ we have that $\eta(t_0)=\eta(t_1)=\dot{\eta}(t_0)=\dot{\eta}(t_1)=0$.