0

I am stucked on how to execute a shell script only once after the machine reboots, the file that I have has the following content where I am writing a test to get only the first element from the file:

#!/bin/bash

echo "A" >> /home/vtr/Documents/test.txt
echo "B" >> /home/vtr/Documents/test.txt 
echo "C" >> /home/vtr/Documents/test.txt

sleep 10  #this sleep I put only to the lines below be executed when the machine came back to the desktop
test=$(sort /home/vtr/Documents/test.txt | uniq | head -n 1)
echo $test >> /home/vtr/Documents/finaltest.txt

I already went through cron and by systemd but on both of them instead have only A as output I am getting a file which was ran several times:

A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A

And this is my /etc/systemd/system/testing.service file:

[Unit]
Description=One test script
After=network.target

[Service]
Type=oneshot
ExecStart=/home/vtr/Documents/testing.sh

[Install]
WantedBy=multi-user.target

I also tried to perform a test only writing "A","B" and "C" without the sleep, test and echo lines above echo "C" >> /home/vtr/Documents/test.txt but I am having the same thing where the output file should be:

A
B
C

But in fact it's printing and running several times also:

A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C

Is there anyway to run a script after reboot once?

1
  • 3
    Do you ever truncate /home/vtr/Documents/test.txt? These "several times" may be from all the previous runs (previous reboots). Side note: this is why you test such things with date instead of echo some fixed string. Apr 17 at 20:07

2 Answers 2

0

First you can simplify your script:

#!/bin/bash

printf "A\nB\nC\n" >> /home/vtr/Documents/test.txt

sleep 10
sort -u /home/vtr/Documents/test.txt |tee /home/vtr/Documents/test.txt | head -1 >> /home/vtr/Documents/finaltest.txt

And if you add in cron records like:

@reboot /home/vtr/Documents/testing.sh

you will have script execured on boot

For testing you ca make the script like this:

printf "A\nB\nC\n" >> /home/vtr/Documents/1.txt
    
    sleep 10
    sort -u /home/vtr/Documents/test.txt >/home/vtr/Documents/2.txt
sort -u /home/vtr/Documents/test.txt|tee /home/vtr/Documents/test.txt >/home/vtr/Documents/3.txt
sort -u /home/vtr/Documents/test.txt |tee /home/vtr/Documents/test.txt | head -1 >> /home/vtr/Documents/4.txt

and check the content of these files. also you can add after first line and make it

#!/bin/bash 
set -x
11
  • I did it now but I am still getting test.txt and finaltest.txt files with lots of A,B,C and A appends on each respective file, the aim is being append only A at the finaltest.txt file and A,B,C at test.txt. What I am thinking weird is why after reboot the script is run several times instead once.
    – zeus
    Apr 17 at 15:08
  • @xts, remove the service and keep only cron. And check in cron log what happen. Will try to align the things Apr 17 at 15:20
  • @xts, check my edited answer Apr 17 at 15:24
  • I think that it didn't work here, when I after did this command: sort -u /home/vtr/Documents/test.txt |tee /home/vtr/Documents/test.txt | head -1 >> /home/vtr/Documents/finaltest.txt at the gnome terminal outside the script it returned me the first three letters, but when I did it again it returned me nothing and when I executed the whole script the text file returned me the same (all void) I don't know why, it is due because the several times that the file is executed after the reboot.
    – zeus
    Apr 17 at 17:16
  • I let only cron active, the systemd I have deleted it.
    – zeus
    Apr 17 at 17:17
0

Have you tried the crontab with @reboot parameter? Perhaps you should add @reboot /path/to/bashscript.sh to the end of your crontab file with crontab -e command. Also change the >> operators in your bash script to > operator and try again.

1
  • The >> has a purpose though. I think the comment shows the culprit of the outcome.
    – stoney
    Apr 24 at 5:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .