Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Execute multiple commands with timer in same shell

Hi guys as the title says i need a way to execute multiple commands in the same shell with a timer. For example let's say i want to open a shell and execute a simple "ls" followed by a "mkdir hello" after 10 seconds from the first command.

Can you provide me some example? thanks :)

This is a very bad piece of code which doesn't do what i want of course.
Code
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cerrno>
#include <unistd.h>
#include <chrono>
#include <thread>

int main() {
   system("gnome-terminal -x sh -c 'ls; mkdir hello; exec bash'");
   return 0;
}

I could use a "usleep" but i don't think it'll work. And i know that "system()" is not very safe to use due to security reasons.

any idea on how should i actually write the code? 🤨
Yeah. Drop the C++ and use shell script.
sadly i really need to do this in c++ ... i know it would be better with shell script but i can't use it :P
(May 20, 2017 01:30 PM)jarvis1 Wrote:  sadly i really need to do this in c++ ... i know it would be better with shell script but i can't use it :P

Why not?
It running in linux? Sorry if it linux i can't help you. But in windows it can be coded it like this

Code
#include <iostream>
#include <windows.h>

using namespace std;

int main() {
system("pause");
system("timeout /10 /nobreak");
system("pause");
return 0;
}
(May 20, 2017 07:36 PM)Annabelle Lee Wrote:  Why not?

It's a school project man, i need to use C++, and yeah i'm on linux so the windows example is not so useful i'm afraid 😁
Code
system("ls; sleep 10; mkdir hello");
[Image: lKhgahK.gif]
Don't use system(..), its blocking and its frowned upon. Write a proper implemention by using the exec family of functions: https://linux.die.net/man/3/exec. You may want to daemonize the part where you're executing the commands too.
I only use https://ricochet.im - ricochet:lmi3tdtlsnziqwdk

Jabber: [email protected]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 238 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[Shell] by thecowmilk_Oct 30, 2021 09:42 AM 0 107 Oct 30, 2021 09:42 AM
Last Post: thecowmilk_
[Shell] by PhloxSep 7, 2017 12:58 PM 1 159 Sep 7, 2017 12:58 PM
Last Post: 0xDEAD10CC