exec Command in Linux

Introduction to Linux exec Command

In this tutorial, we will delve into the Linux exec command and explore its practical applications in system administration. We will begin by understanding the exec system call, a fundamental function used to execute a new program within the context of the current process. This involves replacing the existing process image with the new one. We will then demonstrate how to execute external commands using the exec system call, and also how to manipulate input and output streams using exec(). This comprehensive guide will equip you with the knowledge to effectively utilize the exec command in various Linux scenarios.

Understanding the exec System Call

In this section, we will focus on the exec system call in Linux, a crucial component for process management. The exec system call is invoked to execute a program or command within the current process. Upon execution, the current process image is superseded by a new process image, effectively transforming the process.

To illustrate the exec system call, let's create a basic C program that demonstrates its function:

#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, compile and execute the program using the following commands:

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

Notice that the output only includes the result of the ls -l command. The line "After exec" is not printed. This is because the exec system call replaced the original process image, thus halting the original program's execution.

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

Several variants of the exec system call exist, including execvp, execve, and execvpe. These variants offer different ways to specify the executable file and its associated arguments, allowing systemadmin more flexibility.

Executing External Commands with exec()

This section will demonstrate how to execute external commands using the exec system call in your programs.

The exec system call facilitates the execution of external commands or programs from within a C program. Let's create a simple C program that shows this capability:

#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, compile and run the program using the following commands:

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 effectively replaces the current process with the new process, which in this case is the ls -l command. The line "This line should not be printed." is not executed, as the original program is terminated by the exec call. This is crucial for systemadmin understanding.

You can execute various external commands using the exec system call. For instance, 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 mechanism to integrate system-level functionality into your applications by allowing the execution of external commands from within a C program, often used by a root user or systemadmin.

Redirecting Input and Output with exec()

In this section, we will explore how to redirect the input and output streams of a command executed using the exec system call, an important skill for a Linux systemadmin.

Let's begin by creating a C program that reads input from the user and executes the cat command with the provided 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, 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 utilize the dup2 function to redirect the standard input (stdin) to the user's input. We then execute the cat command, which reads from the redirected stdin and displays the input received.

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. This file will contain the output of the ls -l command.

The dup2 function redirects the standard output (stdout) to the file output.txt. The open function creates the file, assigning the appropriate permissions. This level of control is essential for a systemadmin.

By integrating input and output redirection with the exec system call, you can develop versatile C programs that combine system-level functionality with custom application logic, valuable for tasks on Linux systems.

Summary

In this lab, we explored the Linux exec system call, which enables the execution of a program or command within the context of the current process. Upon execution, the current process image is replaced by the new process image. We demonstrated how to use the exec system call to execute external commands and how to redirect input and output with exec(). Key takeaways include: 1) The exec system call replaces the current process image, effectively terminating the original program. 2) The exec system call facilitates the execution of external commands from within a C program. 3) Input and output redirection can be achieved when using the exec system call, empowering a systemadmin for better control.

400+ Linux Commands