← 返回命令列表

Linux command

sem 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run a command

sem [command]

Run up to 4 jobs

sem -j 4 --id [myid] [command]

Run command in foreground

sem --fg [command]

Wait for all commands

sem --id [myid] --wait

Run jobs matching CPU core count

sem -j +2 [command]

Set a timeout

sem --semaphore-timeout [10] [command]

Parallel compression

for i in *.log; do sem -j+0 gzip $i; done; sem --wait

说明

sem is an alias for GNU parallel --semaphore. It acts as a counting semaphore for executing shell commands in parallel with controlled concurrency. When the maximum number of parallel jobs is reached, sem waits for one to complete before starting the next command. sem is useful for limiting concurrent access to resources, parallelizing loops in shell scripts while preventing system overload, and implementing mutual exclusion for processes that cannot run simultaneously. The semaphore state is stored in ~/.parallel/semaphores/. Unlike GNU parallel, sem does not parse the command arguments for job construction; it simply executes the provided command when the semaphore permits.

参数

--bg
Run command in background. This is the default behavior. sem will not wait for the command to complete before exiting.
--fg
Run command in foreground. sem waits for the semaphore to be available, runs the command, waits for it to complete, then exits.
-j _N_
Allow up to N commands to run in parallel. Default is 1 (mutex mode). Use -j+N to add N to CPU core count, -j-N to subtract, or -jN% to use N% of cores.
--id _name_, --semaphorename _name_
Use name as the semaphore identifier. Default is the name of the controlling tty. Commands with the same id share the same semaphore.
--semaphore-timeout _secs_
If secs > 0: force semaphore acquisition after timeout. If secs < 0: exit if semaphore not available within timeout.
--wait
Wait for all commands in this semaphore to complete before exiting.
--pipe
Pass standard input to the command.
--help
Display help information.
--version
Display version information.

FAQ

What is the sem command used for?

sem is an alias for GNU parallel --semaphore. It acts as a counting semaphore for executing shell commands in parallel with controlled concurrency. When the maximum number of parallel jobs is reached, sem waits for one to complete before starting the next command. sem is useful for limiting concurrent access to resources, parallelizing loops in shell scripts while preventing system overload, and implementing mutual exclusion for processes that cannot run simultaneously. The semaphore state is stored in ~/.parallel/semaphores/. Unlike GNU parallel, sem does not parse the command arguments for job construction; it simply executes the provided command when the semaphore permits.

How do I run a basic sem example?

Run `sem [command]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --bg do in sem?

Run command in background. This is the default behavior. sem will not wait for the command to complete before exiting.