Linux exec Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux exec command and its practical applications. We will start by understanding the exec system call, which is used to execute a program or command in the current process, replacing the current process image with a new process image. We will then demonstrate how to execute external commands using the exec system call, as well as how to redirect input and output with exec(). This lab aims to provide a comprehensive understanding of the exec command and its various use cases in Linux.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/echo -.-> lab-422666{{"`Linux exec Command with Practical Examples`"}} linux/printf -.-> lab-422666{{"`Linux exec Command with Practical Examples`"}} linux/ls -.-> lab-422666{{"`Linux exec Command with Practical Examples`"}} end

Understand the exec System Call

In this step, we will explore the exec system call in Linux. The exec system call is used to execute a program or command in the current process, replacing the current process image with a new process image.

To understand the exec system call, let's start by creating a simple C program that demonstrates its usage:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Before exec\n");
    execl("/bin/ls", "ls", "-l", NULL);
    printf("After exec\n");
    return 0;
}

Save this code in a file named exec_example.c.

Now, let's compile and run the program:

gcc -o exec_example exec_example.c
./exec_example

Example output:

Before exec
total 4
-rwxrwxr-x 1 labex labex 8704 May 30 11:32 exec_example

As you can see, the output only shows the ls -l command output, and the "After exec" line is not printed. This is because the exec system call replaces the current process image with the new process image, effectively terminating the original program.

The exec system call takes the path to the executable file and the arguments to be passed to the new process. In the example above, we used execl to execute the /bin/ls command with the -l argument.

There are several variants of the exec system call, such as execvp, execve, and execvpe, which provide different ways to specify the executable and its arguments.

Execute External Commands with exec()

In this step, we will explore how to execute external commands using the exec system call.

The exec system call can be used to execute external commands or programs from within a C program. Let's start by creating a simple C program that demonstrates this:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Executing 'ls -l' command using exec:\n");
    execl("/bin/ls", "ls", "-l", NULL);
    printf("This line should not be printed.\n");
    return 0;
}

Save this code in a file named exec_command.c.

Now, let's compile and run the program:

gcc -o exec_command exec_command.c
./exec_command

Example output:

Executing 'ls -l' command using exec:
total 12
-rwxrwxr-x 1 labex labex 8704 May 30 11:32 exec_command
-rw-rw-r-- 1 labex labex  241 May 30 11:32 exec_command.c
-rw-rw-r-- 1 labex labex   70 May 30 11:32 exec_example.c

As you can see, the exec system call replaces the current process with the new process, in this case, the ls -l command. The "This line should not be printed." statement is never executed because the original program is terminated by the exec call.

You can also execute different external commands using the exec system call. For example, to execute the echo command:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Executing 'echo Hello, World!' using exec:\n");
    execl("/bin/echo", "echo", "Hello, World!", NULL);
    printf("This line should not be printed.\n");
    return 0;
}

Save this code in a file named exec_echo.c, compile and run it:

gcc -o exec_echo exec_echo.c
./exec_echo

Example output:

Executing 'echo Hello, World!' using exec:
Hello, World!

The exec system call provides a powerful way to execute external commands from within a C program, allowing you to integrate system-level functionality into your applications.

Redirect Input and Output with exec()

In this step, we will learn how to redirect the input and output of a command executed using the exec system call.

Let's start by creating a C program that reads input from the user and executes the cat command with the input:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Enter some text: ");

    // Redirect stdin to the user's input
    dup2(STDIN_FILENO, 0);

    // Execute the 'cat' command to display the input
    execl("/bin/cat", "cat", NULL);

    printf("This line should not be printed.\n");
    return 0;
}

Save this code in a file named exec_redirect_input.c.

Now, let's compile and run the program:

gcc -o exec_redirect_input exec_redirect_input.c
./exec_redirect_input

Example output:

Enter some text: Hello, World!
Hello, World!

In this example, we use the dup2 function to redirect the standard input (stdin) to the user's input. Then, we execute the cat command, which will read from the redirected stdin and display the input.

Next, let's create a program that redirects the output of a command executed with exec:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
    // Redirect stdout to a file
    int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    dup2(fd, STDOUT_FILENO);
    close(fd);

    // Execute the 'ls -l' command with the redirected output
    execl("/bin/ls", "ls", "-l", NULL);

    printf("This line should not be printed.\n");
    return 0;
}

Save this code in a file named exec_redirect_output.c.

Compile and run the program:

gcc -o exec_redirect_output exec_redirect_output.c
./exec_redirect_output

After running the program, you should find a file named output.txt in the current directory, containing the output of the ls -l command.

The dup2 function is used to redirect the standard output (stdout) to the file output.txt. The open function is used to create the file with the appropriate permissions.

By combining input and output redirection with the exec system call, you can create powerful C programs that integrate system-level functionality with custom logic.

Summary

In this lab, we explored the Linux exec system call, which is used to execute a program or command in the current process, replacing the current process image with a new process image. We learned how to use the exec system call to execute external commands, and how to redirect input and output with exec(). The key takeaways are: 1) The exec system call replaces the current process image with a new process image, effectively terminating the original program. 2) The exec system call can be used to execute external commands or programs from within a C program. 3) Input and output can be redirected when using the exec system call.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like