Sample Header Ad - 728x90

Run shell script file once after reboot

0 votes
2 answers
2692 views
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
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
"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?
Asked by zeus (1 rep)
Apr 17, 2023, 02:46 PM
Last activity: Sep 4, 2024, 10:04 PM