How to run a script automatically in linux? The 2019 Stack Overflow Developer Survey Results...

Did any laptop computers have a built-in 5 1/4 inch floppy drive?

Falsification in Math vs Science

What information about me do stores get via my credit card?

How do you keep chess fun when your opponent constantly beats you?

writing variables above the numbers in tikz picture

Deal with toxic manager when you can't quit

How to notate time signature switching consistently every measure

What is the most efficient way to store a numeric range?

RequirePermission not working

What is the motivation for a law requiring 2 parties to consent for recording a conversation

Can we generate random numbers using irrational numbers like π and e?

How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?

Why not take a picture of a closer black hole?

Alternative to の

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

Worn-tile Scrabble

Does HR tell a hiring manager about salary negotiations?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

How to add class in ko template in magento2

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

What do these terms in Caesar's Gallic Wars mean?

"as much details as you can remember"

Why is the maximum length of OpenWrt’s root password 8 characters?



How to run a script automatically in linux?



The 2019 Stack Overflow Developer Survey Results Are InInserting a date and time of execution of a bash script into a filerun bash script from another script and redirect its outputShell script file (.sh) does not run, and throws an errorWhere can i get nohup.out, if script starts at startupLinux script to extract usernames from text fileudevadm trigger works but automatically running *.sh script not workinglftp run jar file after mirror has changed files?What is the difference between 'test' and 'test.sh' while running shell scripts?howto create script to customize configuration of Ubuntu/LinuxWhy cron not executing script?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







4















I use linux command line and I'm a beginner with it.
I've created two files, test.sh and test.log.
the process is any output going to test.log and i get it successfully the output in test.log.
I want to run a script automatically every 5 seconds and write only if there is any change with the .sh file.



test.sh contains :



#!/bin/bash
while [ true ] ;
do
echo "" date and Time >> test.log
date +%x_r >> test.log
lsusb >> test.log

sleep 5;
done


My question: Is there any way to run it automatically and only append the new change in the file with the new date? for example if anyone insert USB device into my machine it will append it with the new date into the existing log file it.










share|improve this question

























  • This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

    – Simon Aronsson
    Nov 16 '16 at 17:25


















4















I use linux command line and I'm a beginner with it.
I've created two files, test.sh and test.log.
the process is any output going to test.log and i get it successfully the output in test.log.
I want to run a script automatically every 5 seconds and write only if there is any change with the .sh file.



test.sh contains :



#!/bin/bash
while [ true ] ;
do
echo "" date and Time >> test.log
date +%x_r >> test.log
lsusb >> test.log

sleep 5;
done


My question: Is there any way to run it automatically and only append the new change in the file with the new date? for example if anyone insert USB device into my machine it will append it with the new date into the existing log file it.










share|improve this question

























  • This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

    – Simon Aronsson
    Nov 16 '16 at 17:25














4












4








4


0






I use linux command line and I'm a beginner with it.
I've created two files, test.sh and test.log.
the process is any output going to test.log and i get it successfully the output in test.log.
I want to run a script automatically every 5 seconds and write only if there is any change with the .sh file.



test.sh contains :



#!/bin/bash
while [ true ] ;
do
echo "" date and Time >> test.log
date +%x_r >> test.log
lsusb >> test.log

sleep 5;
done


My question: Is there any way to run it automatically and only append the new change in the file with the new date? for example if anyone insert USB device into my machine it will append it with the new date into the existing log file it.










share|improve this question
















I use linux command line and I'm a beginner with it.
I've created two files, test.sh and test.log.
the process is any output going to test.log and i get it successfully the output in test.log.
I want to run a script automatically every 5 seconds and write only if there is any change with the .sh file.



test.sh contains :



#!/bin/bash
while [ true ] ;
do
echo "" date and Time >> test.log
date +%x_r >> test.log
lsusb >> test.log

sleep 5;
done


My question: Is there any way to run it automatically and only append the new change in the file with the new date? for example if anyone insert USB device into my machine it will append it with the new date into the existing log file it.







bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '16 at 15:49









amc

4,81462746




4,81462746










asked Nov 16 '16 at 15:17









GhassanGhassan

265




265













  • This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

    – Simon Aronsson
    Nov 16 '16 at 17:25



















  • This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

    – Simon Aronsson
    Nov 16 '16 at 17:25

















This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

– Simon Aronsson
Nov 16 '16 at 17:25





This seems like an unnecessarily complicated way to do it. You should check the manpages for cron

– Simon Aronsson
Nov 16 '16 at 17:25










2 Answers
2






active

oldest

votes


















1














This should work, it stores the last output of lsusb in $lastoutput and appends if they're if not equal



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done





share|improve this answer


























  • Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

    – Ghassan
    Nov 16 '16 at 18:29











  • Yes its in the terminal , but the result (new change) should go to .log file but does not .

    – Ghassan
    Nov 16 '16 at 20:55











  • @Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

    – IanC
    Nov 16 '16 at 23:49











  • My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

    – Evan Chen
    Nov 16 '16 at 23:54











  • @Ghassan can you try the new code?

    – Evan Chen
    Nov 17 '16 at 0:07



















-1














You have to:




  • put your file in /etc/init.d directory

  • make your sh file executable (use the chmod +x command)


If it doesn't work properly, create a symlink of your file to /etc/rc.d/:



ln -s /etc/init.d/test.sh /etc/rc.d/ 


May that helps :)






share|improve this answer


























  • The question asks how to automatically detect change, not how to start on boot

    – Evan Chen
    Nov 16 '16 at 18:19












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f850143%2fhow-to-run-a-script-automatically-in-linux%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This should work, it stores the last output of lsusb in $lastoutput and appends if they're if not equal



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done





share|improve this answer


























  • Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

    – Ghassan
    Nov 16 '16 at 18:29











  • Yes its in the terminal , but the result (new change) should go to .log file but does not .

    – Ghassan
    Nov 16 '16 at 20:55











  • @Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

    – IanC
    Nov 16 '16 at 23:49











  • My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

    – Evan Chen
    Nov 16 '16 at 23:54











  • @Ghassan can you try the new code?

    – Evan Chen
    Nov 17 '16 at 0:07
















1














This should work, it stores the last output of lsusb in $lastoutput and appends if they're if not equal



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done





share|improve this answer


























  • Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

    – Ghassan
    Nov 16 '16 at 18:29











  • Yes its in the terminal , but the result (new change) should go to .log file but does not .

    – Ghassan
    Nov 16 '16 at 20:55











  • @Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

    – IanC
    Nov 16 '16 at 23:49











  • My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

    – Evan Chen
    Nov 16 '16 at 23:54











  • @Ghassan can you try the new code?

    – Evan Chen
    Nov 17 '16 at 0:07














1












1








1







This should work, it stores the last output of lsusb in $lastoutput and appends if they're if not equal



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done





share|improve this answer















This should work, it stores the last output of lsusb in $lastoutput and appends if they're if not equal



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '16 at 23:53

























answered Nov 16 '16 at 16:29









Evan ChenEvan Chen

74449




74449













  • Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

    – Ghassan
    Nov 16 '16 at 18:29











  • Yes its in the terminal , but the result (new change) should go to .log file but does not .

    – Ghassan
    Nov 16 '16 at 20:55











  • @Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

    – IanC
    Nov 16 '16 at 23:49











  • My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

    – Evan Chen
    Nov 16 '16 at 23:54











  • @Ghassan can you try the new code?

    – Evan Chen
    Nov 17 '16 at 0:07



















  • Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

    – Ghassan
    Nov 16 '16 at 18:29











  • Yes its in the terminal , but the result (new change) should go to .log file but does not .

    – Ghassan
    Nov 16 '16 at 20:55











  • @Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

    – IanC
    Nov 16 '16 at 23:49











  • My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

    – Evan Chen
    Nov 16 '16 at 23:54











  • @Ghassan can you try the new code?

    – Evan Chen
    Nov 17 '16 at 0:07

















Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

– Ghassan
Nov 16 '16 at 18:29





Thanks , but in this way it will not save the new change in .log file ,, i try it and it's running in the terminal not in the .log file

– Ghassan
Nov 16 '16 at 18:29













Yes its in the terminal , but the result (new change) should go to .log file but does not .

– Ghassan
Nov 16 '16 at 20:55





Yes its in the terminal , but the result (new change) should go to .log file but does not .

– Ghassan
Nov 16 '16 at 20:55













@Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

– IanC
Nov 16 '16 at 23:49





@Ghassan does the user running the script have enough permissions to write in the test.log file? Could it be that maybe you created the file as root and is now running it as your regular user?

– IanC
Nov 16 '16 at 23:49













My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

– Evan Chen
Nov 16 '16 at 23:54





My apologies, I did not realize that the code doesn't work (I was typing on my phone and I wasn't able to test it). I fixed it now, it should work

– Evan Chen
Nov 16 '16 at 23:54













@Ghassan can you try the new code?

– Evan Chen
Nov 17 '16 at 0:07





@Ghassan can you try the new code?

– Evan Chen
Nov 17 '16 at 0:07













-1














You have to:




  • put your file in /etc/init.d directory

  • make your sh file executable (use the chmod +x command)


If it doesn't work properly, create a symlink of your file to /etc/rc.d/:



ln -s /etc/init.d/test.sh /etc/rc.d/ 


May that helps :)






share|improve this answer


























  • The question asks how to automatically detect change, not how to start on boot

    – Evan Chen
    Nov 16 '16 at 18:19
















-1














You have to:




  • put your file in /etc/init.d directory

  • make your sh file executable (use the chmod +x command)


If it doesn't work properly, create a symlink of your file to /etc/rc.d/:



ln -s /etc/init.d/test.sh /etc/rc.d/ 


May that helps :)






share|improve this answer


























  • The question asks how to automatically detect change, not how to start on boot

    – Evan Chen
    Nov 16 '16 at 18:19














-1












-1








-1







You have to:




  • put your file in /etc/init.d directory

  • make your sh file executable (use the chmod +x command)


If it doesn't work properly, create a symlink of your file to /etc/rc.d/:



ln -s /etc/init.d/test.sh /etc/rc.d/ 


May that helps :)






share|improve this answer















You have to:




  • put your file in /etc/init.d directory

  • make your sh file executable (use the chmod +x command)


If it doesn't work properly, create a symlink of your file to /etc/rc.d/:



ln -s /etc/init.d/test.sh /etc/rc.d/ 


May that helps :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '16 at 23:05









SYN

556212




556212










answered Nov 16 '16 at 17:25









Dodi lilaDodi lila

6116




6116













  • The question asks how to automatically detect change, not how to start on boot

    – Evan Chen
    Nov 16 '16 at 18:19



















  • The question asks how to automatically detect change, not how to start on boot

    – Evan Chen
    Nov 16 '16 at 18:19

















The question asks how to automatically detect change, not how to start on boot

– Evan Chen
Nov 16 '16 at 18:19





The question asks how to automatically detect change, not how to start on boot

– Evan Chen
Nov 16 '16 at 18:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f850143%2fhow-to-run-a-script-automatically-in-linux%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Why do type traits not work with types in namespace scope?What are POD types in C++?Why can templates only be...

Will tsunami waves travel forever if there was no land?Why do tsunami waves begin with the water flowing away...

Should I use Docker or LXD?How to cache (more) data on SSD/RAM to avoid spin up?Unable to get Windows File...